1

我在 Stackoverflow 上为 WooCommerce 找到了一个代码,如果订单低于设定值,则允许添加额外费用。

(在本例中,我的值为 10,低于该值的所有内容都会将差额作为加工税添加)

如果订单总和超过该设定值,我想隐藏订单页面中的文本。

这是代码:

function woo_add_cart_fee() {

    global $woocommerce;

    $subt = $woocommerce->cart->subtotal;

    if ($subt < 10 ) { 
        $surcharge = 10 - $subt;
    } else { 
        $surcharge = 0;
    }   

    $woocommerce->cart->add_fee( __('Procesing tax for under 10 dolars', 'woocommerce'), $surcharge );

}

add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

谢谢

4

1 回答 1

1

global$woocommerce不是必需的,因为您可以访问$cart.

添加费用可以包含在 if 条件中

function woo_add_cart_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Get subtotal
    $subt = $cart->get_subtotal();

    // Below
    if ($subt < 10 ) {
        $surcharge = 10 - $subt;

        $cart->add_fee( __( 'Procesing tax for under 10 dolars', 'woocommerce' ), $surcharge );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee', 10, 1 );
于 2020-05-25T10:06:13.430 回答