【问题标题】:WooCommerce dynamic minimum order amount based feeWooCommerce 基于动态最低订单金额的费用
【发布时间】:2017-08-18 16:47:10
【问题描述】:

我需要在购物车中设置最低订单费用,因此如果购物车中的产品总额不超过 10 英镑,则需要支付额外费用以将价格提高到 10 英镑。

这是我目前拥有的代码,它在购物车阶段运行良好,但是当您到达结帐时,定价部分由于某种原因不会停止加载并且您无法结帐,有人可以帮忙吗?

functions.php 中的代码:

function woocommerce_custom_surcharge() {
  global $woocommerce;
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    $minimumprice = 10;
    $currentprice = $woocommerce->cart->cart_contents_total;
    $additionalfee = $minimumprice - $currentprice;
    if ( $additionalfee >= 0 ) {
        wc_print_notice(
            sprintf( 'We have a minimum %s per order. As your current order is only %s, an additional fee will be applied at checkout.' ,
                wc_price( $minimumprice ),
                wc_price( $currentprice )
            ), 'error'
        );
        $woocommerce->cart->add_fee( 'Minimum Order Adjustment', $additionalfee, true, '' );
    }
}
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );

【问题讨论】:

    标签: php wordpress woocommerce checkout cart


    【解决方案1】:

    2019 年 5 月增强并更新

    您遇到的无限加载旋转问题是由于 wc_print_notice()woocommerce_cart_calculate_fees 挂钩中使用时造成的。这似乎是一个错误。

    如果你改用wc_add_notice(),问题就消失了但通知会显示2次。

    此外,我还重新访问了您的代码。唯一的解决方案是将其拆分为 2 个单独的函数(第三个用于消息)

    // Add a custom fee (displaying a notice in cart page)
    add_action( 'woocommerce_cart_calculate_fees', 'add_custom_surcharge', 10, 1 );
    function add_custom_surcharge( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_cart_calculate_fees' ) >= 2 )
            return;
    
        $min_price     = 100; // The minimal cart amount
    
        $current_price = $cart->cart_contents_total;
        $custom_fee    = $min_price - $current_price;
    
        if ( $custom_fee > 0 ) {
            $cart->add_fee( __('Minimum Order Adjustment'), $custom_fee, true );
    
            // NOTICE ONLY IN CART PAGE
            if( is_cart() ){
                wc_print_notice( get_custom_fee_message( $min_price, $current_price ), 'error' );
            }
        }
    }
    
    // Displaying the notice on checkout page
    add_action( 'woocommerce_before_checkout_form', 'custom_surcharge_message', 10 );
    function custom_surcharge_message(  ) {
        $min_price     = 100; // The minimal cart amount
    
        $cart          = WC()->cart;
        $current_price = $cart->cart_contents_total;
        $custom_fee    = $min_price - $current_price;
    
        // NOTICE ONLY IN CHECKOUT PAGE
        if ( $custom_fee > 0 ) {
            wc_print_notice( get_custom_fee_message( $min_price, $current_price ), 'error' );
        }
    }
    
    // The fee notice message
    function get_custom_fee_message( $min_price, $current_price ) {
        return sprintf(
            __('We have a minimum %s per order. As your current order is only %s, an additional fee will be applied.', 'woocommerce'),
            wc_price( $min_price ),
            wc_price( $current_price )
        );
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 2019-07-29
      • 2020-05-11
      • 2014-12-22
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 2017-07-15
      • 2020-11-26
      相关资源
      最近更新 更多