【问题标题】:Trying to hide the add fee text if criteria is met in WooCommerce如果在 WooCommerce 中满足条件,则尝试隐藏添加费用文本
【发布时间】:2020-09-11 22:42:33
【问题描述】:

我在 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】:

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

    【讨论】:

    • 哇,太好了,不仅文字消失了,而且费用是税前计算的。非常感谢!
    • 我刚刚看到代码中的一个错误,我对价值低于 50 的订单收取运费,对超过 50 的订单免费,但使用此代码,免费送货不再起作用了
    • 这是如何确定的?通过自定义代码或通过 WooCommerce 设置或额外的插件?
    • 我不知道为什么它为几个订单这样做,然后我什么也没做,它工作正常。再次感谢您!
    猜你喜欢
    • 2016-11-06
    • 2017-09-19
    • 2015-07-09
    • 2014-02-03
    • 2015-09-12
    • 2017-09-15
    • 2021-08-03
    • 2020-05-10
    • 1970-01-01
    相关资源
    最近更新 更多