简体   繁体   中英

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.

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;

    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM