Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1290782
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI'm hoping someone can't point me to what I am overlooking.
CIRCUMSTANCE:
We have some unique business logic whereby a custom user meta field affects the appropriate tax class. We have three customer "types" (which is this user meta field, say 'type_one'
, 'type_two'
, 'type_three'
) with three corresponding custom Tax Classes ('class_one'
, 'class_two'
, 'class_three'
).
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. Sort of (but not exactly, of course) like this:
function reassign_tax_class( $tax_class, $product ) {
$user = wp_get_current_user();
if(get_user_meta( $user->ID, '_our_custom_field', true)=='type_one'){
$tax_class = 'class_one';
}
return $tax_class;
}
add_filter( 'woocommerce_product_get_tax_class', 'reassign_tax_class', 1, 2 );
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 on an order item. It seems to be in the calculate_taxes() method in the class-wc-order-item.php class (line 220: .php#L220 )--and surely enough if insert line 221 there like $calculate_tax_for['tax_class'] = 'class_one'
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.
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI'm hoping someone can't point me to what I am overlooking.
CIRCUMSTANCE:
We have some unique business logic whereby a custom user meta field affects the appropriate tax class. We have three customer "types" (which is this user meta field, say 'type_one'
, 'type_two'
, 'type_three'
) with three corresponding custom Tax Classes ('class_one'
, 'class_two'
, 'class_three'
).
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. Sort of (but not exactly, of course) like this:
function reassign_tax_class( $tax_class, $product ) {
$user = wp_get_current_user();
if(get_user_meta( $user->ID, '_our_custom_field', true)=='type_one'){
$tax_class = 'class_one';
}
return $tax_class;
}
add_filter( 'woocommerce_product_get_tax_class', 'reassign_tax_class', 1, 2 );
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 on an order item. It seems to be in the calculate_taxes() method in the class-wc-order-item.php class (line 220: https://github/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'] = 'class_one'
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.
Share Improve this question edited Jun 9, 2021 at 15:52 vancoder 7,92428 silver badges35 bronze badges asked Jun 9, 2021 at 0:46 user19918user19918 311 silver badge7 bronze badges 3 |1 Answer
Reset to default 1Posting the solution that worked for me in case it helps anyone in the future (Credit to Rup & WooCommerce Community Slack)
I was filtering the tax class getter on the cart side and that was doing what I needed, so I was focused on figuring out how to also filter the tax class getter on the order side. Devinsays on Community Slack recommended the obvious-in-hindsight solution of filtering also the setter on the cart side and then the order side just works without any filter. In other words, when I re-assign the tax class for the calculation on the cart side, actively set it on order creation. Then it will be attached to the order item for the recalculate later. Something like this:
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);
本文标签: plugin developmentWooCommerce change Tax Class programmatically when Recalculating an existing Order
版权声明:本文标题:plugin development - WooCommerce change Tax Class programmatically when Recalculating an existing Order 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741515162a2382841.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$this->get_prop( 'tax_class', $context );
. So your filter ought to still work I'd expect? Or maybe you need an equivalent woocommerce_order_item_get_tax_class, I don't know how the filters are constructed. – Rup Commented Jun 9, 2021 at 11:11