在 WooCommerce 3 中收取有条件的送货费

Charge a conditional Delivery fee in WooCommerce 3

这是我的情况:

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

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

目前这是我的代码:

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' );
    } 

}

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

我做错了什么?

问题出在你的病情上。应该改为:

If subtotal <= amount && subtotal > amount2

另外你的代码有点老了,试试这个(累进收费的例子):

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+ 上测试并且有效。