Monday 26 October 2020

Change checkout currency based on the product category

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;
}


from Change checkout currency based on the product category

No comments:

Post a Comment