-->

Taxes applied based on cart item quantity in Wooco

2019-07-29 11:39发布

问题:

Is there a way to create a tax class that applies to a product based on the quantity of this product in the cart.

Example: If there is less then 6 items of the same product the taxes applies otherwise the taxes doesn't applies.

Any help is appreciated.

回答1:

It is is possible.

First create in WooCommerce Tax settings a tax class named for example "Zero Rate" like:

1) in Tax options sections add "Zero Rate" and save:

2) A tab "Zero rate" appear. Under this tab section set the tax to zero:

The code:

add_action( 'woocommerce_before_calculate_totals', 'apply_conditionally_taxes', 20, 1 );
function apply_conditionally_taxes( $cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    foreach( $cart->get_cart() as $cart_item ){
        if( $cart_item['quantity'] >= 6 ){
            $cart_item['data']->set_tax_class('zero-rate');
        } else {
            $cart_item['data']->set_tax_class('');
        }
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.