如果在WooCommerce中满足条件,试图隐藏添加费用的文字。

问题描述 投票:1回答: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' );

谢谢你

php wordpress woocommerce cart fee
1个回答
1
投票

全局 $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 );
© www.soinside.com 2019 - 2024. All rights reserved.