I need to change the cart and checkout currency based on the product category. So if the category is "clothing", then the currency on the cart and checkout should be EUR (and charge needs to happen in EUR). Otherwise, if "clothing" category is not attached to the product, then to stay in default USD currency.
This code basically changes the default currency for "clothing’ category, but only on the product page, it doesn’t change on cart and checkout. I need to change it on the cart and checkout as well, and the charge needs to happen in that currency.
add_filter('woocommerce_currency', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency ) {
global $post, $product;
if ( has_term( 'clothing', 'product_cat' ) ) {
return 'EUR';
}else{
return $currency;
}
}
I understand that cart and checkout only allow to have one currency per product. So setting some limitation to allow only one product per cart can happen like this:
add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_only_one_in_cart', 99, 2 );
function bbloomer_only_one_in_cart( $passed, $added_product_id ) {
wc_empty_cart();
return $passed;
}
4
Answers
Maybe, here is a solution (I can’t try it) :
PS: it won’t do the job on product-list pages.
I think this is the answer to your question, although it is not the recommended way because it won’t work on the product list page. Please try with this code and let me know your thought.
I’ve done a bit of research, and there are quite a few additional complexities when dynamically changing currency on a per-category or per-product basis.
The cart, I’m assuming, would still need to calculate a total, and provide 1 checkout currency. Which means, the total would have to be converted as well.
Your best answer will be found digging through this plugin’s code … but I warn you, it’s much more complex than changing the symbol.