WooCommerce税收过滤器未触发

时间:2019-11-22 作者:GO VEGAN

当总和小于100时,我想应用零税,但这段代码没有被激发(我看不到打印到控制台或echo是源代码)。我将其添加到代码片段插件中。我做错了什么?

add_filter( \'woocommerce_product_tax_class\', \'big_apple_get_tax_class\', 1, 2 );

function big_apple_get_tax_class( $tax_class, $product ) {
        echo "<script>console.log(\'Debug Objects\' );</script>";

    global $woocommerce;
    $cart_subtotal = $woocommerce->cart->get_cart_subtotal();
    if ( $cart_subtotal <= 100 )
        $tax_class = \'Zero Rate\';

    return $tax_class;
}

1 个回复
最合适的回答,由SO网友:Chetan Vaghela 整理而成

这里是Link 这可能对你有用。我已经测试过了,对我来说效果很好。您只需注意woocommerce设置https://prnt.sc/q6fse9. 例如是否含税输入的价格。

add_action( \'woocommerce_before_calculate_totals\', \'apply_conditionally_zero_tax_rate\', 10, 1 );
function apply_conditionally_zero_tax_rate( $cart ) {
    if ( is_admin() && ! defined( \'DOING_AJAX\' ) )
        return;

    if ( did_action( \'woocommerce_before_calculate_totals\' ) >= 2 )
        return;

    $defined_amount = 100;
    $subtotal = 0;

    // Loop through cart items (1st loop - get cart subtotal)
    foreach ( $cart->get_cart() as $cart_item ) {
        $subtotal += $cart_item[\'line_total\'];
    }

    // Targeting cart subtotal up to the "defined amount"
    if ( $subtotal > $defined_amount )
        return;

    // Loop through cart items (2nd loop - Change tax rate)
    foreach ( $cart->get_cart() as $cart_item ) {
        $cart_item[\'data\']->set_tax_class( \'zero-rate\' );
    }
}

相关推荐