mercredi 24 janvier 2018

Woocommerce - change currency symbol based on product category

I am trying to change the default Woocommerce currency 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?

Aucun commentaire:

Enregistrer un commentaire