【问题标题】:Add a calculated fee based on total after discounts in WooCommerce在 WooCommerce 中根据折扣后的总额添加计算费用
【发布时间】:2018-03-02 00:53:18
【问题描述】:

我正在尝试在应用折扣后根据小计向我的 woocommerce 购物车添加费用:

    add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = 0.01;
    $surcharge =  $woocommerce->cart->subtotal - $woocommerce->cart->get_cart_discount_total(); 
    $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );

}

我不相信像$woocommerce->cart->get_cart_discount_total() 这样的调用可以在动作挂钩中使用,这就是为什么我不断收到0.00 的费用。

我还阅读了一些不推荐使用的 WC 值并且将始终显示为零,但它没有解释为什么这些数量出现在过滤器中而不是操作中。

我还可以在操作中使用什么来获得相同的数字并添加百分比费用?

【问题讨论】:

    标签: php wordpress woocommerce cart fee


    【解决方案1】:

    WC_Cart 对象参数包含在 woocommerce_cart_calculate_fees 操作挂钩中。我还使用百分比金额计算,因为我想您只是在代码中忘记了它。

    所以你应该试试这个:

    add_action( 'woocommerce_cart_calculate_fees','wc_custom_surcharge', 10, 1 );
    function wc_custom_surcharge( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        // HERE set your percent rate
        $percent = 1; // 1%
    
        // Fee calculation
        $fee = ( $cart->subtotal - $cart->get_cart_discount_total() ) * $percent / 100;
    
        // Add the fee if it is bigger than O
        if( $fee > 0 )
            $cart->add_fee( __('Surcharge', 'woocommerce'), $fee, true );
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件中。

    经过测试,完美运行。

    注意:同样global $woocommerce;$woocommerce->cart 已经被WC()->cart 取代了很长时间。 WC() woocommerce 对象已经包含自己 global $woocommerce;...


    具体更新:

    add_action( 'woocommerce_cart_calculate_fees','wc_custom_surcharge', 10, 1 );
    function wc_custom_surcharge( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        // HERE set your percent rate and your state
        $percent = 6;
        $state = array('MI');
    
        $coupon_total = $cart->get_discount_total();
    
        // FEE calculation
        $fee = ( $cart->subtotal - $coupon_total ) * $percent / 100;
    
        if ( $fee > 0 && WC()->customer->get_shipping_state() == $state )
            $cart->add_fee( __('Tax', 'woocommerce'), $fee, false);
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件中。

    经过测试并且有效。

    【讨论】:

    • 您的代码帮助我发现 get_cart_discount_total 不适用于自定义折扣类型。我为礼品卡添加了自定义折扣类型,它确实在表格和后端显示为优惠券,但未计入折扣总额。有什么我可以在这里做的吗?
    • @mnguyen 请在最后添加您的自定义折扣代码在您的问题中,编辑它...完成后通知我
    • 我通过您个人资料中的表格向您发送了一条消息,并附上此 URL 以识别我的身份。有一个指向开发环境的链接可以查看。谢谢
    【解决方案2】:

    我用来创建优惠券的插件连接到 after_calculate_totals 并在之后调整金额。在插件之前触发的任何东西都不计入调整后的总数。我能够使用插件中的变量调用特定金额来创建我需要的费用金额

    对于其他感兴趣的人:我正在使用 ignitewoo 礼券 pro 插件,并希望根据优惠券后的余额收取费用。这是 Loic 的代码,做了一些修改:

    add_action( 'woocommerce_cart_calculate_fees','wc_custom_surcharge', 10, 1 );
    function wc_custom_surcharge( $cart) {
        global $woocommerce;
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        $state      = array('MI');
    
        // HERE set your percent rate
        $percent = 6;
        $coupTotal = 0;
        foreach ( $woocommerce->cart->applied_coupons as $cc ) {
        $coupon = new WC_Coupon( $cc );
        $amount = ign_get_coupon_amount( $coupon );
        $coupTotal += $amount;
        }
    
    
        // Fee calculation
        $fee = ($cart->subtotal - $coupTotal) * $percent/100;
    if (( $fee > 0 ) AND  (in_array( WC()->customer->shipping_state, $state ) ))
            $cart->add_fee( __('Tax', 'woocommerce'), $fee, false);
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-02
      • 2021-11-23
      • 2023-03-30
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多