在WooCommerce 3中收取有条件的送货费 [英] Charge a conditional Delivery fee in WooCommerce 3

查看:28
本文介绍了在WooCommerce 3中收取有条件的送货费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况,例如:

如果小计< =一定金额&&小计< =一定金额

If subtotal <= certain amount && subtotal <= certain amount

因此,如果小计符合此条件,那么我想收取送货费,该费用也应显示在电子邮件发票中.

So if the subtotal matches this criteria then I want to charge a delivery fee which should also show in the email invoice.

当前,这是我的代码:

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

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

    $subtotal = $woocommerce->cart->subtotal;
    if(subtotal <= certain amount && subtotal <= certain amount){
        $woocommerce->cart->add_fee( 'Delivery Fees', 5, true, 'standard' );
    } 

}

但是它没有显示任何东西.

But it is not showing any thing.

我做错了什么?

推荐答案

问题来自您的情况.应该改为:

The problem comes from your condition. it should be instead:

如果小计< =金额&&小计>数量2

您的代码也有些旧,请尝试这种方式(累进费用示例):

Also your code is a bit old, try it this way (an example of progressive fee):

add_action( 'woocommerce_cart_calculate_fees','my_delivery_fee', 10, 1 );
function my_delivery_fee( $wc_cart ) {

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

    $subtotal = $wc_cart->subtotal;
    $fee = 0;

    // Progressive fee
    if(subtotal < 25 ){ // less than 25 
        $fee = 5;
    } elseif( subtotal >= 25 && subtotal < 50){ // between 25 and 50
        $fee = 10;
    } else { // From 50 and up 
        $fee = 15;
    }

    if( fee > 0 )
        $wc_cart->add_fee( 'Delivery Fees', $fee, true, 'standard' );
}

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

所有代码都在Woocommerce 3+上进行了测试,并且可以正常工作.

All code is tested on Woocommerce 3+ and works.

这篇关于在WooCommerce 3中收取有条件的送货费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆