{"id":3476,"date":"2022-05-09T11:42:37","date_gmt":"2022-05-09T11:42:37","guid":{"rendered":"https:\/\/scriptfeeds.com\/blog\/?p=3476"},"modified":"2022-12-06T21:59:42","modified_gmt":"2022-12-06T21:59:42","slug":"how-to-change-woocommerce-tax-based-on-cart-value-of-the-customer","status":"publish","type":"post","link":"https:\/\/scriptfeeds.com\/blog\/wordpress\/how-to-change-woocommerce-tax-based-on-cart-value-of-the-customer\/","title":{"rendered":"How to change WooCommerce tax based on cart value of the customer"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Recently one of our client asked that &#8220;How he can edit WooCommerce tax based on the cart value of the customer?&#8221; Let say if the cart value is less than X then tax should be X-1 or if cart value id more than X then tax value is X+. How in this article we gonna let you know you can set different tax rates conditionally based on cart item prices in WooCommerce.<\/p>\n\n\n\n<h2 class=\"has-medium-font-size wp-block-heading\">Lets say you selling three products on your WooCommerce store which price as below:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Product A : $200<\/li>\n\n\n\n<li>Product B : $400<\/li>\n\n\n\n<li>Product C : $800<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So if a customer add all the three products on his\/her cart then tax per product will be applied as per the TAX classes you defined in WooCommerce TAX settings. But if you wanna add 5% TAX on cart value is total amount less than or equal to $1000 and 10% TAX if cart value is above $1000 then you can follow the steps below.<\/p>\n\n\n\n<h3 class=\"has-medium-font-size wp-block-heading\">Step by Step guide to change WooCommerce tax based on cart value<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create two WooCommerce TAX class based on requirement in WooCommerce TAX settings. Here i created &#8220;GST 12%&#8221; and &#8220;GST 18%&#8221; for example. <a href=\"https:\/\/scriptfeeds.com\/blog\/wordpress\/gst-billing-in-woocommerce\/\">Click here<\/a> to know how to create TAX classes in WooCommerce.<\/li>\n\n\n\n<li>Now open the functions.php file of your Child theme in WordPress theme editor. Create custom function hooked in <strong>woocommerce_before_calculate_totals<\/strong> and add below code<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action( 'woocommerce_before_calculate_totals', 'apply_conditionally_gst12_tax_rate', 10, 1 );\n\nfunction apply_conditionally_gst12_tax_rate( $cart ) {\n     \n\n    if ( did_action( 'woocommerce_before_calculate_totals' ) &gt;= 2 )\n        return;\n\n    $defined_amount = 1000;\n    $subtotal = 0;\n\n    foreach ( $cart-&gt;get_cart() as $cart_item ) {\n    $subtotal += $cart_item&#91;'line_total'];\n    }\n       \n    if ( $subtotal &gt;= $defined_amount ){\n\n      foreach ( WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $cart_item ) {\n      $cart_item&#91;'data']-&gt;set_tax_class( 'gst-12' );\n      }\n    }  \n}\n\nadd_action( 'woocommerce_before_calculate_totals', 'apply_conditionally_gst5_tax_rate', 10, 1 );\n\nfunction apply_conditionally_gst5_tax_rate( $cart ) {\n     \n    if ( did_action( 'woocommerce_before_calculate_totals' ) &gt;= 2 )\n        return;\n\n    $defined_amount = 1000;\n    $subtotal = 0;\n\n    foreach ( $cart-&gt;get_cart() as $cart_item ) {\n    $subtotal += $cart_item&#91;'line_total'];\n    }\n       \n    if ( $subtotal &lt; $defined_amount ){\n\n      foreach ( WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $cart_item ) {\n      $cart_item&#91;'data']-&gt;set_tax_class( 'gst-5' );\n      }\n    }  \n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Works and tested on WooCommerce version : 6.5.0<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color wp-block-paragraph\">NOTE : Above code will work only if the TAX setting is exclusive of tax, means product prices are exclusive of tax<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So that&#8217;s all you need to do. Try the code above and let us know how it works.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Happy Coding!<\/p>\n\n\n<p>[caldera_form id=&#8221;CF60ca2e3ccf64f&#8221;]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently one of our client asked that &#8220;How he can edit WooCommerce tax based on the cart value of the customer?&#8221; Let say if the cart value is less than X then tax should be X-1 or if cart value id more than X then tax value is X+. How in this article we gonna [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3393,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":{"subtitle":"","format":"standard","video":"","gallery":"","source_name":"","source_url":"","via_name":"","via_url":"","override_template":"0","override":[{"template":"2","single_blog_custom":"","parallax":"1","fullscreen":"1","layout":"right-sidebar","sidebar":"default-sidebar","second_sidebar":"default-sidebar","sticky_sidebar":"1","share_position":"float","share_float_style":"share-monocrhome","show_share_counter":"1","show_view_counter":"1","show_featured":"1","show_post_meta":"1","show_post_author":"1","show_post_author_image":"1","show_post_date":"1","post_date_format":"default","post_date_format_custom":"Y\/m\/d","show_post_category":"1","show_post_reading_time":"0","post_reading_time_wpm":"300","show_zoom_button":"0","zoom_button_out_step":"2","zoom_button_in_step":"3","show_post_tag":"1","show_prev_next_post":"0","show_popup_post":"0","number_popup_post":"1","show_author_box":"0","show_post_related":"1","show_inline_post_related":"0"}],"override_image_size":"0","image_override":[{"single_post_thumbnail_size":"no-crop","single_post_gallery_size":"crop-500"}],"trending_post":"0","trending_post_position":"meta","trending_post_label":"Trending","sponsored_post":"0","sponsored_post_label":"Sponsored by","sponsored_post_name":"","sponsored_post_url":"","sponsored_post_logo_enable":"0","sponsored_post_logo":"","sponsored_post_desc":"","disable_ad":"0"},"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[86,75],"tags":[208,209],"class_list":["post-3476","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-woocommerce","category-wordpress","tag-conditional-tax-rate-of-woocommerce","tag-woocommerce-tax-based-on-cart-value"],"_links":{"self":[{"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/posts\/3476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/comments?post=3476"}],"version-history":[{"count":0,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/posts\/3476\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/media\/3393"}],"wp:attachment":[{"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/media?parent=3476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/categories?post=3476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/tags?post=3476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}