skip to Main Content

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


  1. Maybe, here is a solution (I can’t try it) :

    add_filter('option_woocommerce_currency', function ($currency) {
        // is current page is product-single
        if(is_single('product')) {
            return has_term('clothing', 'product_cat') ? 'EUR' : $currency;
        }
        // for every else pages
        global $woocommerce;
        foreach($woocommerce->cart->get_cart() as $product) {
            if (has_term('clothing', 'product_cat', $product['product_id'])) {
                return 'EUR';
            }
        }
        return $currency;
    }, 10, 1);
    

    PS: it won’t do the job on product-list pages.

    Login or Signup to reply.
  2. add_filter('woocommerce_currency', 'change_existing_currency_symbol', 10, 2);
    function change_existing_currency_symbol( $currency ) {
    global $post, $product;
    $defaultCurrency = 'EUR';
    $categoriesCurrencies = [ 
              'audio'=>'EUR', 
              'mobile'=>'LEK'
    ];
     return $categoriesCurrencies[$product->category] ?: $defaultCurrency;
    }
    
    Login or Signup to reply.
  3. 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.

    add_filter('woocommerce_currency', 'change_existing_currency_symbol', 10, 1);
    function change_existing_currency_symbol( $currency ) {
    
        if ( is_product() ) {
            if ( has_term( 'clothing', 'product_cat' ) ) return 'EUR';
        } else if ( is_cart() || is_checkout() ) {
            global $woocommerce;
            foreach ( $woocommerce->cart->get_cart() as $product ) {
                if ( has_term('clothing', 'product_cat', $product['product_id'] ) ) return 'EUR';
            }
        }
    
        return $currency;
    }
    
    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search