0

I am trying to change the tax for the user who will fill in the "tax" field. But for some reason the tax does not change. Although when using such a condition with the default city field WC()->customer->get_billing_city() == 'xxx'), everything will change.

add_action( 'woocommerce_before_calculate_totals', 'custom_cart_items_tax_prices', 10, 1 );
function custom_cart_items_tax_prices( $cart  ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    if ( WC()->customer->get_billing_city() == 'xxx') {
        foreach ( $cart->get_cart() as $cart_item ) {
            $product_id = $cart_item['data']->get_id();
            if ($product_id==11) {
                $cart_item['data']->set_tax_class( 'Test' );
            }
        }
    }
}

What kind of mistake could there be?

  • With `WC_Product` `set_tax_class()` method, you need to use the tax class **slug**, not the name. You can find it in any admin single product settings while inspecting the code of the related dropdown (select field) to set the tax class. See it [in this answer](https://stackoverflow.com/a/44964389/3730754) for example. – LoicTheAztec Aug 03 '23 at 13:53

0 Answers0