1
votes

I'm trying to change the Woocommerce's product default currency based on the category.

Like if "Audio" is a category then set the EUR to be the currency.

I've tried the code below but it's not working:

function change_woocommerce_currency( $currency ) {
    if ( is_product_category( 'audio' ) ) {
        $currency = 'EUR'; 
    }
    return $currency;
}
add_filter( 'woocommerce_currency', 'change_woocommerce_currency' );

The currency price needs to be changed on the product page as well that currency to be on the cart and checkout page, and the charge needs to happen in that category's currency.

4
what error do you get?Michael Welch
did any of the answers worked for you?Ozgur Sar

4 Answers

1
votes

First, you can create a custom field with ACF to store EUR regular prices (For example: regular_price_eur)

Then, you can filter the price and change its currency symbol based on the category.

function filter_price( $price, $product ) {
  $price_in_eur = get_field( 'regular_price_eur', $product->get_id() ); // get EUR price from a custom field

  if ( $price_in_eur && is_product_category( 'audio' ) ) {
    return absint( $price_in_eur );
  } else {
    return $price;
  }
}
add_filter( 'woocommerce_product_get_price', 'filter_price', 10, 2 );

function change_currency_symbol( $currency ) {
  if ( is_product_category( 'audio' ) )  {
    return 'EUR';
  }

  return $currency;
}
add_filter( 'woocommerce_currency', 'change_currency_symbol' );
0
votes

Please try this

add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
    function change_existing_currency_symbol( $currency_symbol, $currency ) {
        global $post, $product;
    
        if ( has_term( 'audio', 'product_cat' ) ) {
            switch( $currency ) {
                 case 'USD': $currency_symbol = 'EUR'; 
                 break;
            }
        }
        return $currency_symbol; // <== HERE
    }
0
votes
function change_woocommerce_currency( $currency ) {
    $defaultCurrency = 'EUR';
    $categories = [
       'audio'  => 'EUR',
       'mobile' => 'LEK',  
     ];
    $productCategory = 'audio'; // get product's category
    return $categories[$productCategory] ?: $defaultCurrency;
}
add_filter( 'woocommerce_currency', 'change_woocommerce_currency' );
-1
votes

You have various ways to do that. First, you can use a plugin, such as "WooCommerce Currency per Product", this lets you add a specific currency to any item you want.

If you want to do it by code, this is how i would have done it :

function change_woocommerce_currency( $currency_symbol, $currency ) {
  if ( is_product_category( 'audio' ) ) {
    switch( $currency ) {
      case 'USD': $currency_symbol = 'whatever you want'; 
      break;
      }
    }
    return $currency_symbol;
}
add_filter( 'woocommerce_currency', 'change_woocommerce_currency' );