Asif Ameer
Asif Ameer

Reputation: 95

Add custome text after price in woocommerce product page

I am working on salient theme for my E-commerce website. I want to show "B" after price in woocommerce product page. The page displays like "$99,99"
I want to make it displays like this: "$99,99 TEXT"

I have the code for it :

function change_product_price( $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' );

Now I want to know where to add the code in woo-commerce directory. Any help would be appreciated.

thanks

Upvotes: 4

Views: 32673

Answers (3)

Efstef
Efstef

Reputation: 98

Tested for woocommerce 6.x.x - 2022

IMPORTANT: If you have enabled the tax calculation (woocommerce -> Settings -> Enable Tax) inside woocommerce, you have the option from the admin panel to add whatever you want after the price (suffix). This option can be found inside the Admin dashboard and under the woocommerce -> Settings -> Tax.

The code snippets provided below can be added inside the file functions.php of your child's theme folder. I would strongly recommend you to use a child theme (if provided by the theme or create one) so that when you will update your theme, none of these changes will get erased.

Woocommerce provides a filter, for quite some time now, that you can add a price suffix, and that's what you want in your case.

add_filter( 'woocommerce_get_price_suffix', 'add_some_text_after_price', 99, 4 );

function add_some_text_after_price ($html, $product, $price, $qty){
    $html .= ' TEXT'; //You can write whatever you want here. Just stay inside the ' '
    return $html;
}

Also, as the other answers suggested, the function's name that is inside your filter is not the same as the function's name that you do your action.

The name of your function: change_product_price

And inside your filters that you want to add this action, you call: cw_change_product_price_display

So nothing will happen and nothing will work, like the function, you call I nowhere to be found.

Also except for adding a suffix, you can also add a prefix using one of the filters that you already use (in case someone needs something like that)

add_filter( 'woocommerce_get_price_html', 'my_function', 99, 2 );)

function my_function( $price, $product ){
    $price = 'Text ' . $price;
    return $price;
}

General Knowledge

The 99 inside the filters is the priority number that the filter will start, before any other filter. That means that if you want to start some filters before others, then you can change the priority. The second number is the number of the arguments ($price, $html, ...) that you put inside your function.

Both numbers are needed in most cases.

Upvotes: 3

Mykyta Dudariev
Mykyta Dudariev

Reputation: 462

Add this code inside functions.php of your theme:

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' );

Upvotes: 8

andy Lr
andy Lr

Reputation: 21

The name of the function you're calling in add_filter isn't the same as the name of your actual function. It seems you've forgotten the _display in the function name.

In the following code, the function is name cw_change_product_price_display and cw_change_product_price_display is called in add_filter

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' );

Upvotes: 2

Related Questions