DistrictWeb
DistrictWeb

Reputation: 19

Change applied taxes rate based on cart item quantity in Woocommerce

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.

Upvotes: 1

Views: 2188

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253901

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:

enter image description here

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

enter image description here

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;

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

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

Upvotes: 4

Related Questions