Title: Change Tax Class Programatically
Last modified: August 7, 2019

---

# Change Tax Class Programatically

 *  Resolved [Neji87](https://wordpress.org/support/users/neji87/)
 * (@neji87)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/change-tax-class-programatically/)
 * I’m trying to set up a select box which allows my site visitors to change the
   tax rate that they need to pay in WooCommerce (version 3.6.4).
 * I’ve set up a custom plugin which detects if they’ve changed the tax rate, then
   updates the tax class used. This is persisted by use of an additional cookie.
   It’s working well, however, in two areas, the price is always out of date on 
   a page reload.
 * On the basket page, they see a form (with just a select and a submit button) 
   if they submit the form, this POSTs to the same URL and sets the cookie and the
   tax class by using a couple of filters. Here’s the code doing this (a little 
   messy, and verbose, granted):
 *     ```
       <?php
   
       namespace Example;
   
       class TaxRate
       {
           public function __construct()
           {
               //$this->setCookie();
   
               $this->addActions();
   
               $this->addFilters();
           }
   
           private function addActions()
           {
               add_action("init", [$this, 'setCookie']);
           }
   
           public function setCookie()
           {
               if($_POST['tax-rate'] == "wholesale")
               {
                   setcookie("tax-rate", "wholesale", time() + (86400 * 365), "/");
   
                   $_COOKIE['tax-rate'] = "wholesale";
               }
               else if($_POST['tax-rate'] == "standard")
               {
                   setcookie("tax-rate", "standard", time() + (86400 * 365), "/");
   
                   $_COOKIE['tax-rate'] = "standard";
               }
               else if(!isset($_COOKIE['tax-rate']))
               {
                   setcookie("tax-rate", "standard", time() + (86400 * 365), "/");
   
                   $_COOKIE['tax-rate'] = "standard";
               }
           }
   
           private function addFilters()
           {
               add_filter("woocommerce_product_get_tax_class", [$this, 'setTaxRate'], 1, 2);
               add_filter("woocommerce_product_variation_get_tax_class", [$this, 'setTaxRate'], 1, 2);
           }
   
           public function setTaxRate($tax_class, $product)
           {
               if($_COOKIE['tax-rate'] == "wholesale")
               {
                   return "Wholesale";
               }
   
               return "";
           }
       }
       ```
   
 * When the basket/cart page reloads, the prices in the table are updated. However,
   in the header, we have the subtotal displayed but this displays the old amount(
   using the standard tax class). If I refresh the page or load it again, the new
   amount (using the selected tax class) displays.
 * Variations prices on the product page are also a problem, they always show the
   standard rate price, rather than the ones set in the filters.
 * I can’t figure out why this is the case although I’m suspecting it’s down to 
   the cookie and the use of the filters that get the tax class. Also, after a little
   digging, the cart total in the header looks to use the get_cart_subtotal in the
   WC_Cart class.
 * Thanks in advance!

Viewing 1 replies (of 1 total)

 *  Plugin Support [slash1andy](https://wordpress.org/support/users/slash1andy/)
 * (@slash1andy)
 * Automattic Happiness Engineer
 * [6 years, 7 months ago](https://wordpress.org/support/topic/change-tax-class-programatically/#post-11969359)
 * Hey there!
 * Since this is a few weeks old, and about customization, I am going to mark as
   resolved. A lot of people have found help in the Advanced WooCommerce group with
   things like this, so I would recommend checking there, or we highly recommend
   contacting one of the services on our Customizations page ([https://woocommerce.com/customizations/](https://woocommerce.com/customizations/))

Viewing 1 replies (of 1 total)

The topic ‘Change Tax Class Programatically’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [slash1andy](https://wordpress.org/support/users/slash1andy/)
 * Last activity: [6 years, 7 months ago](https://wordpress.org/support/topic/change-tax-class-programatically/#post-11969359)
 * Status: resolved