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
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' );
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' );
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.
Related
The prices in my woocommerce store have the words "starting from" automatically added in front of them and I want to remove these words.
This happens on all products both variable and regular products. I need a code that will allow me to completely remove the words that come before the price.
screen shots:
screen shot - regular product
screen shot - variable product
Thanks.
To remove "starting from" prefix from displayed prices, try the following code in function.php file of your active child theme (or active theme).
add_filter( 'woocommerce_get_price_html', 'filter_get_price_html_callback', 10, 2 );
function filter_get_price_html_callback( $price, $product ) {
return str_replace( __('starting from'), '', $price );
}
Similarly for single product pages:
add_filter( 'woocommerce_get_price_html', 'filter_get_price_html_callback', 10, 2 );
function filter_get_price_html_callback( $price, $product ) {
if ( is_product() )
$price = str_replace( __('starting from'), '', $price );
return $price;
}
Note: Please replace 'starting from' according to your need.
I am trying to prepend a Woocommerce single product page title (in the header) with the category so it looks like
Category
Product Title
This is what I have so far:
function prepend_category_to_title($title, $id) {
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->name;
break;
}
if( is_product() ) {
$title = $product_cat . $title;
}
return $title;
}
add_filter('the_title', 'prepend_category_to_title', 10, 2);
I can get it to prepend the title fine but using the_title outputs the category name to every single menu item and every single breadcrumb as well.
I see from this: https://wordpress.stackexchange.com/questions/309151/apply-the-title-filter-in-post-page-title-but-not-in-menu-title that this is expected behaviour and I can add further filters to remove these extra outputs. e.g. add_filter( 'pre_wp_nav_menu'...
But this seems horribly inefficient and messy. Additionally, prepending the title this way means that the category text also gets wrapped in the title <h1> tags which I don't want as the category name needs to be above the product title in smaller <h5> tags (and different font).
To make this slightly more complex, due to the theme I am using (Astra), the product title sits in a header outside of Woocommerce's
woocommerce_single_product_summary hook.
How can I prepend the product title with the product category in a more elegant way? TIA!
Yes it is based on hook so it will not create an issue. But if you need to proceed based on WooCommerce way then you need to skip WordPress hook the_title and need to use WooCommerce hook.
woocommerce_single_product_summary hook is associate with title, price, expert, .. on single product page. see below code of line that shows hook and there related function.
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
To go with Woocommerce, you need to remove the actual hook and reinitate the function like below. Hope below code justify your question.You can put this snippet in your theme's functions.php file
remove_action('woocommerce_single_product_summary','woocommerce_template_single_title',5);
add_action('woocommerce_single_product_summary', 'woocommerce_my_single_title',5);
if ( ! function_exists( 'woocommerce_my_single_title' ) ) {
function woocommerce_my_single_title() {
?>
/*
your logic goes here
No need to return data, just echo/print
*/
<?php
}
}
I would like to use a pods shortcode field inside the woocommerce variable product description but by default the variable description field does not support shortcodes.
The variation description is stored in an array woocommerce_available_variation, so I can't simple call the function do_shortcode($variation).
I am trying to allow short codes in this field by using the below code:
add_filter( 'woocommerce_available_variation', 'shortcode_variation_description');
function shortcode_variation_description( $variation ) {
$variation['variation_description'] = do_shortcode( $variation['variation_description'] );
return $variation;
But it's not working. Why?
When using your code, it works. To test, I have used the Woocommerce shortcode [products] in a variation description as follow:
The imputed text is (where 37 is a real simple product ID):
"This is a description with a shortcode… [products ids="37"] As you can see this shorcode is detected and displayed."
And I get this display:
So it works for real. I have lightly made some little changes to this code version (yours work too):
add_filter( 'woocommerce_available_variation', 'variation_description_allow_shortcodes', 10, 3 );
function variation_description_allow_shortcodes( $variation_data, $product, $variation ) {
$variation_data['variation_description'] = do_shortcode( $variation_data['variation_description'] );
return $variation_data;
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
You can define your own shortcode, see the documentation. You can copy/paste the code because it's wrapped in a class so it won't trigger fatal error due to double declaration.
But shortcode do not work that way :
function shortcode_handler($atts) {
//code goes here
}
add_shortcode("name_of_shortcode","shortcode_handler');
I thought this would have been easy, but I am stuck. All I am trying to do is add the word each after the variation price on a product page. The solution I have found adds it on the category page and in two places on the product page.
The code is:
/* Adds a text Each - after price */
function change_product_price( $price ) {
$price .= ' each';
return $price;
}
add_filter( 'woocommerce_get_price_html', 'change_product_price' );
From the picture above, I only need the each added to the price above the add to cart button, but not the other pacles like the crossed out section in the photo above.
Thank you for any guidance you can provide.
The following code will add a suffix to the product variations price:
add_filter('woocommerce_available_variation', 'variation_price_custom_suffix', 10, 3 );
function variation_price_custom_suffix( $variation_data, $product, $variation ) {
$variation_data['price_html'] .= ' <span class="price-suffix">' . __("each", "woocommerce") . '</span>';
return $variation_data;
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
Is it possible to use the wp_filter_kses() function to remove the removal of html syntax in woocommerce price suffix input?
Thanks.
With the help of hook below price html could be changed as per your expectation.
Here is the code:
add_filter( 'woocommerce_price_html', 'price_custom_class', 10, 2 );
function price_custom_class( $price, $product ){
// your code goes here
return $price;
}