88532

Taxes applied based on cart item quantity in Woocommerce

Question:

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

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

Any help is appreciated.

Answer1:

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:

<a href="https://i.stack.imgur.com/30Eih.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/30Eih.png" data-original="https://i.stack.imgur.com/30Eih.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

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

<a href="https://i.stack.imgur.com/agrV0.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/agrV0.png" data-original="https://i.stack.imgur.com/agrV0.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

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.

Recommend

  • Having trouble initiating an array in a structure
  • Pass by Value and Pass by Reference
  • WooCommerce creating Product Custom Attributes fields programatically?
  • Gmail API quota usage check
  • Disable tax programmatically for a specific user role
  • html/css: how to create a hexagonal image-placeholder
  • jquery add an event handler to objects in an array
  • How to update powerpivot pivot table filter via cell reference?
  • Single virtual attribute definition for multiple fields
  • iOS custom UTI in UIDocumentPickerViewController initWithDocumentTypes
  • How to get file download speed (transfer rate) with php?
  • Is there any purpose for h2-h6 headings in HTML5?
  • .NET video play library which allows to change the playback rate?
  • Aptana 3 remove bundle (jquery)
  • What does “t” refer to in this SQL?
  • How do I retrieve the user information of a user authenticated with Apache's mod_ldap?
  • CERN ROOT exporting data to plain text
  • Uncaught TypeError: $(…).select2 is not a function
  • Django simple Captcha “No module named fields” error
  • Alternative to overridePendingTransition() - Android
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • How to get Eclipse Oxygen to run on Java 9
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Why does access(2) check for real and not effective UID?
  • Deleting and Updating values from a cusrsor adapter
  • Modifying destination and filename of gulp-svg-sprite
  • SSO with signing and signature validation doesn't work
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • XCode can't find symbols for a specific iOS library/framework project
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Change div Background jquery
  • Android Google Maps API OnLocationChanged only called once
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory
  • UserPrincipal.Current returns apppool on IIS
  • Net Present Value in Excel for Grouped Recurring CF