如果在 WooCommerce 中满足条件,则尝试隐藏添加费用文本

Trying to hide the add fee text if criteria is met in WooCommerce

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

(在这个例子中,我的值为 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' );

谢谢

全局 $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 );