taxjar / taxjar-woocommerce-plugin

WooCommerce Sales Tax Plugin by TaxJar
http://www.taxjar.com/woocommerce-sales-tax-plugin/
GNU General Public License v2.0
28 stars 29 forks source link

Transients Cache doesn't update upon user_exemption_type update. #243

Open ghost-writer-727 opened 1 year ago

ghost-writer-727 commented 1 year ago

If a user builds a cart while is Non Exempt then realizes they should be exempt and calls my company to have it fixed, we can change them to a valid exempt type ('other'). However, the cart remains taxed as long as the address, products and quantities remain the same.

We have tried updating the quantities and then putting them back. We have tried clearing the cart, all cookies and all browser cache, then logging back in and when any previous combination of address, products and quanities return the previous tax calculation is also returned.

I believe the cleanest solution would be to include the user's tax status in the $request_body property of the Tax_Calculator class. This way when when the transient key is set, it is unique based on whether or not the user is exempt or not.

ghost-writer-727 commented 1 year ago

I actually see that the request body object already supports an exemption type. The issue is that there isn't one being passed to it. However, this hook makes it easy to force the exemption type. Here's my current solution that works well and still allows for caching.

function force_set_cart_exemption_type( $exemption_type, $cart ){
      if( $cart ){
          if( $customer = $cart->get_customer() ){
              if( $user_id = $customer->get_id() ){
                  $exemption_type = get_user_meta( $user_id, 'tax_exemption_type', true );
              }
          }
      }
      return $exemption_type;
}
add_filter( 'taxjar_cart_exemption_type', 'force_set_cart_exemption_type', 10, 2 );