Change applied taxes rate based on cart item quantity in Woocommerce

后端 未结 1 1406
温柔的废话
温柔的废话 2021-01-22 17:07

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 item

相关标签:
1条回答
  • 2021-01-22 17:49

    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.

    0 讨论(0)
提交回复
热议问题