Allow shortcodes in product variations description in Woocommerce - 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');

Related

How to auto copy title into description field for all WooCommerce variation products?

I have lots of variable products and the description field is left blank by default, but I have other plugins which are set to display the Variation Description field on the front end. How can I get my site to copy the variation name (Pink Set No Box, Pink Set With Box, Green Set No Box etc.) into the corresponding variation description field?
As well as doing it for all existing products, it would need to do it for all new products being added as well.
So how can I copy automatically for product variations, the title into description field in WooCommerce?
Instead of copying the variation product name in the variation description, you can assign it to the description dynamically using this very simple hooked function:
add_filter( 'woocommerce_product_variation_get_description', 'wc_product_variation_get_description_filter', 10, 2 );
function wc_product_variation_get_description_filter( $description, $product ){
return $product->get_name();
}
Or you can also append it to the description using:
add_filter( 'woocommerce_product_variation_get_description', 'wc_product_variation_get_description_filter', 10, 2 );
function wc_product_variation_get_description_filter( $description, $product ){
return $description . $product->get_name();
}
Or prepend it with:
add_filter( 'woocommerce_product_variation_get_description', 'wc_product_variation_get_description_filter', 10, 2 );
function wc_product_variation_get_description_filter( $description, $product ){
return $product->get_name() . $description;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
On some cases when products have been created since a long time, the attributes are not displayed in the product variation name… You will need to add the following line:
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_true' );
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Related:
How to display the variation name in Woocommerce Items
Remove attribute values from product variation title and show them on separate rows

Add a text under specific Woocommerce product category archive page

I would like to show text under Shop page main content. Different text for different categories.
I am aware of showing category description on the Shop page for each category but this is already in use.
Right now I am this far but it does not seem to work in the Child theme functions.php
if ( is_category( 'category1' ) ) {
function add_my_text() {
print '<p>This is my extra text.</p>';
}
add_action( 'woocommerce_after_main_content', 'add_my_text' );
}
Will be thankful if anyone knows how to improve this function
Thrilled I found this answer. Was struggling mightily to add content based on a custom taxonomy created with Custom Post Type UI. In case anyone else needs to know, you don't have to change permalinks or anything silly like that. Simple modification of LoicTheAztec's answer above does the trick.
add_action( 'woocommerce_product_meta_end', 'add_my_text' );
function add_my_text() {
if ( wc_get_product_class( $class = 'taxonomy-term' ) ) {
echo '<h2>IT WORKS!!!</h2>';
}
}
If you want to insert content into a different place on the page, choose a different hook from this very helpful WooCommerce Visual Hook Guide.
Hope this saves somebody else 9 hours :)
The Wordpress conditional function is_category() doesn't work with Woocommerce product categories which are a custom taxonomy. Instead use Woocommerce conditional tag is_product_category() inside your hooked function like:
add_action( 'woocommerce_after_main_content', 'add_my_text' );
function add_my_text() {
if ( is_product_category( 'category1' ) ) {
echo '<p>This is my extra text.</p>';
}
}
Code goes on function.php file of your active child theme (or active theme). It should works.

Add an extra text that appears on selected variations in WooCommerce

I would like to add a function that fires when the customer selects a product variant. Just as the price appears (after choosing variants).
It is a simple function (from functions.php) that reads additional information about productions from the database.
However, I do not know if there is such a hook?
To add a text on selecting a variation you can use the following:
add_filter('woocommerce_available_variation', 'variation_custom_text', 10, 3 );
function variation_custom_text( $variation_data, $product, $variation ) {
// Here your custom text
$custom_text = __("This is my custom text…", "woocommerce");
$variation_data['availability_html'] .= '<p>' . $custom_text . '</p>';
return $variation_data;
}
Code goes in function.php file of the active child theme (or active theme). Tested and works.

Add a shortcode under variation selectors in Woocommerce single variable product

I would like to add a shortcode under variation selector and above add to cart button so it would open up a size chart, just like here:
I got the shortcode, but I don't know what code to use to make it appear in that specific area.
Try the following (before the selected variation price):
add_action( 'woocommerce_before_single_variation', 'add_shortcode_before_single_variation', 20 );
function add_shortcode_before_single_variation(){
global $product;
// HERE comes your shortcode
echo do_shortcode( "[my_shortcode attr='blabla']" );
}
OR this one instead (after the selected variation price):
add_action( 'woocommerce_single_variation', 'add_shortcode_before_single_variation', 15 );
function add_shortcode_before_single_variation(){
global $product;
// HERE comes your shortcode
echo do_shortcode( "[my_shortcode attr='blabla']" );
}
Code goes in function.php file of your active child theme (or theme).
it should work (replacing my fake shortcode with yours)…

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
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.

Categories