questions

How can add suffix to both regular price and on sale price in WooCommerce?

Rate this post

I want to add ,- after the sale price and after the normale price in WooCommerce.
The function I am using right now is:

// Add ,- after price
add_filter( 'woocommerce_get_price_html', 'njengah_text_after_price' );
function njengah_text_after_price($price){
     $text_to_add_after_price  = ',-'; //change text in bracket to your preferred text    
    return $price .   $text_to_add_after_price;   
} 

This is working but it only adds after the sale price in WooCommerce. It should be added after the standard price as well.

Here is the HTML how its added right now:

<span class="price">
    <del>
        <span class="woocommerce-Price-amount amount">
            <bdi>
                <span class="woocommerce-Price-currencySymbol">€</span>
                4.326
            </bdi>
        </span>
    </del> 
    <ins>
        <span class="woocommerce-Price-amount amount">
            <bdi>
                <span class="woocommerce-Price-currencySymbol">€</span>
                2.294
            </bdi>
        </span>
    </ins>
,-
</span>

Thanks for your time!

 

✔️Solution:

By default the price suffix is added after the <ins> (in case the product is on sale) element so it is shown only once.

If you want to show the suffix at both the regular price and the sale price you will need to use the hook filters:

So:

// adds the suffix to the price
add_filter( 'woocommerce_get_price_suffix', 'add_price_suffix' );
function add_price_suffix(){
    $suffix = ',-';
    update_option( 'woocommerce_price_display_suffix', $suffix );
    return $suffix;
}

// adds the suffix to the regular price (the <del> element) if the product is on sale
add_filter( 'woocommerce_format_sale_price', 'change_format_sale_price', 99, 3 );
function change_format_sale_price( $price_html, $regular_price, $sale_price ) {
    $suffix = get_option( 'woocommerce_price_display_suffix' );
    $price_html = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . $suffix . '</del> <ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins>';
    return $price_html;
}

The code has been tested and works. Add it to your active theme’s functions.php.

Leave a Reply

Your email address will not be published.

Back to top button