Reputation: 1462
I am trying to change the default Woocommerce currency symbol based on the product category.
My default WC currency is set to USD and all my products display with '$'
prefix before the price. But instead of '$'
, I would like to show '$$$'
only for the products that are in 'clearance'
category.
This is my code:
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
global $post, $product, $woocommerce;
if ( has_term( 'clearance', 'product_cat' ) ) {
switch( $currency ) {
case 'USD': $currency_symbol = '$$$'; break;
}
return $currency_symbol;
}
}
It works, and '$$$' is displayed only for the products within the 'clearance' category, however it removes the '$' from all products in the remaining categories.
I need that if statement to do nothing if the condition is not met.
I've also tried with endif
closing tag like this:
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
global $post, $product, $woocommerce;
if ( has_term( 'clearance', 'product_cat' ) ) :
switch( $currency ) {
case 'USD': $currency_symbol = '$$$'; break;
}
return $currency_symbol;
endif;
}
but same thing here. It shows '$$$'
for all products within 'clearance' category, but removes the '$'
for any other product.
What am I doing wrong?
Upvotes: 2
Views: 2277
Reputation: 1
Creating a child theme for the current theme can be the solution but I think you don't have to do that much if you only want to change symbol.
Simply go to the woocommerce back end files ..
wp-content/plugins/woocommerce/includes/wc-core-functions.php
If you don't have back end file excess download file manager in your Wordpress theme you will see these folder in you main site.
wp-content>>plugins>>woocommerce>>includes>>wc-core-functions.php
In wc-core-function.php
file you will see the symbol
Upvotes: 0
Reputation: 254363
Related: Custom cart item currency symbol based on product category in Woocommerce 3.3+
You need to put return $currency_symbol;
outside the if
statement this way:
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( 'clearance', 'product_cat' ) ) {
switch( $currency ) {
case 'USD': $currency_symbol = '$$$';
break;
}
}
return $currency_symbol; // <== HERE
}
Code goes in function.php file of your active child theme (or active theme).
Now it should work.
Upvotes: 2