• Resolved theseforumslookquitehelpful

    (@theseforumslookquitehelpful)


    Hello. I’m hoping someone can’t point me to what I am overlooking.

    CIRCUMSTANCE:
    We have some odd business logic whereby a custom usermeta field affects the appropriate tax class. We have three customer “types” (which is this user meta field) with three corresponding custom Tax Classes. When we calculate taxes on a Cart, we run a filter on woocommerce_product_get_tax_class and re-assign the Tax Class based on looking up this user meta. It works perfectly for our needs.

    PROBLEM:
    We have frequent need to use the Recalculate button on the edit Order page in WP Admin. I cannot find where I can filter or hook the tax class assignment. It seems to be in the calculate_taxes() method in the class-wc-order-item.php class (line 220: https://github.com/woocommerce/woocommerce/blob/trunk/includes/class-wc-order-item.php#L220 )–and surely enough if insert line 221 there like $calculate_tax_for[‘tax_class’] = ‘somethg_specific’ it does exactly what I am looking to do.

    Is there a way I can do this without hacking core?

    Thanks vastly for any help. I’m at a complete loss and against a clock.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support con

    (@conschneider)

    Engineer

    Hi there,

    We have frequent need to use the Recalculate button on the edit Order page in WP Admin. I cannot find where I can filter or hook the tax class assignment.

    There are number of hooks provided by WooCommerce, when you are clicking the Recalculate button. Have a look at: https://stackoverflow.com/questions/56701407/woocommerce-add-order-admin-hook-for-recalculate-button

    I would try the following:

    * Choose an appropriate hook.
    * Then instantiate WC_Order_Item::calculate_taxes() (probably not statically, but you get my gist).
    * And pass $calculate_tax_for argument (since calculate_taxes() accepts it).

    Kind regards,

    Thread Starter theseforumslookquitehelpful

    (@theseforumslookquitehelpful)

    Thanks so much, @conschneider ! (I’ve bookmarked that stackoverflow answer as a handy resource.)

    What you suggest would absolutely work. I think for my specific purposes, I’ve implemented an even simpler solution.

    I was filtering the tax class getter on the cart end and it was working great on checkout, so I was focused on filtering the tax class getter on the order end in the same way. What hadn’t occurred to me (ugh) is that I could filter the getter and the setter on the cart end, and then the tax class would just be there for the Recalculate on the order end without my needing to filter anything at all.

    Sort of like so, if it is useful to anyone else in the future:

    function also_reassign_the_setter($item, $cart_item_key, $values, $order){
        //some custom logic
        $item->set_tax_class('class_one');
    }
    add_action('woocommerce_checkout_create_order_line_item', 'also_reassign_the_setter', 20, 4);

    Anyway, thanks for offering a solution (that works as well.) I’m most grateful!

    Plugin Support con

    (@conschneider)

    Engineer

    Hi again,

    Glad to hear that was helpful :).
    Thanks a lot for posting your findings, approach and code here, appreciate it!

    All the best going forward,
    Con

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Change Tax Class programmatically when Recalculating an existing Order’ is closed to new replies.