Taxes applied based on cart item quantity in Woocommerce

两盒软妹~` 提交于 2019-12-24 08:57:57

问题


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.



来源:https://stackoverflow.com/questions/50956044/taxes-applied-based-on-cart-item-quantity-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!