Search code examples
phpwordpresswoocommerceproduct

Add a custom text before the price display in WooCommerce


In WooCommerce, I'm using this code to put a text in the price display:

function cw_change_product_price_display( $price ) {
    $price .= ' TEXT';
    return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );

The page displays like "$99,99 TEXT"

I want to make it displays like this: "TEXT $99,99"

Thank you for the help.


Solution

  • You have just to inverted the price and the text:

    add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
    add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
    function cw_change_product_price_display( $price ) {
        // Your additional text in a translatable string
        $text = __('TEXT');
    
        // returning the text before the price
        return $text . ' ' . $price;
    }
    

    This should work as you expect…