Site logo

Archived topic

Reuse product discription using elements

14 replies · Started by Diederick on December 22, 2021

Viewing posts 1–15 of 15

Hello,

I'm wondering if this is possible using the elements feature.
Since we have a lot of products we use the same description for each category.
Now we add this manually to each product.
Would it be possible to add this automatically with elements?
Only tricky part is the product title is used in the description.
So we need to add that using a shortcode or something...
Any suggestions?

Hi there,

is this for a Product Category Archives ?

Hello David,

No it's for the short description on the product page.
e.g.: I want all the toys in category cars to display - this "%title%" car is a car from brand X.
I want all toys in category dolls to display - this "%title%" doll is doll from brand x.

This site shows the various hooks in the woocommerce single product template:

https://www.businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/

The Short Description is hooked in here ( you can see it in the hook list below the template mockup ):

add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );

So you can remove the default short description by adding this PHP Snippet to your site:

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );

Then create a Hook for: woocommerce_template_single_excerpt to add in your own content.

Before going there - how many different categories do you have?
And is this category specific short description been stored anywhere? Such as in the Product Category Term Meta ?

Hi David,

O oké that would be an option but would require a lot of coding.
Our site used 5 languages and we have over 20 types of categories.
Currently we do this manually for each product.
I'm already using the elements to hook in on the short description to showcase the brands logo on top of the text.
And I also have a hook after the additional information hook that showcases the brands story.
No idea how many hooks we can use before we get a slowdown.
I've added a link to the product page of one of our products so you can see what I mean.

Simplest option would be to include your custom description in the same Hook as your Brand Logo. At least it keeps those two elements in one place.

Then you just need to get the product name - you can find a snippet for that here:

https://stackoverflow.com/a/63280622

Let us know if you need a hand converting that into a shortcode.

Hi David,

I guess that would solve it.
Never made shortcodes before but I guess that will be no problem.
If this shortcode is called from a element on a product page will it know that it's on that product page?
Since it will not be possible to send an attribute with it.
I'll try to do this in a few days.
You just made me to curious to wait :D

For the shortcode you can add this PHP Snippet to your site:

add_shortcode( 'product_name', function() {
    ob_start();
  
    global $product;
    // If the WC_product Object is not defined globally
    if ( ! is_a( $product, 'WC_Product' ) ) {
        $product = wc_get_product( get_the_id() );
    }
    echo $product->get_name();
  
    return ob_get_clean();
});

This doc explains adding PHP:
https://docs.generatepress.com/article/adding-php/

And then you can add this shortcode to the element: [product_name]
If it works correctly it should grab the name of the current product :)

Hi David,

The shortcode works.
That already saves me some time.
Now I want to test this on the elements.
When I create an element and add the shortcode I get a fatal error when I save it.
I cannot edit this element anymore.
Strange thing is the element works.
So I only get a fatal error if I want to edit the element.
The line of code where he gets the error is:
$product_title = $product->get_name();

I also noticed that when I want to show a text using the elements on a specific category that works.
But I have products that are in multiple categories.
So Sometimes I get more then one text that is added.
Is there a way only the text of the main category is shown?
Or do I need to exclude all the other categories?

Hi David,

After a good night sleep I found a bypass for the shortcode.
I check if it's admin and send a dummy string then.
Else I just do the shortcode.
So That works.

Now Do you have any idea on my second question?

Is there a way only the text of the main category is shown?
Or do I need to exclude all the other categories?

Unless theres a way to differentiate between a Category and a Main Category then you would need to Exclude all un-required Categories.

Hi David,

Hmm, the main category is something from Woocommerce I guess.
I need to select that when I select multiple categories.
Don't know if that is something we can get in the elements.

I don't think thats a core feature of Woocommerce. Are you using any plugins to add that ?

Hmmm gets awfully messy trying to do this.
As it would require adding a PHP Snippet for each and everyone of those Elements to check if the Main Category == X.

If you want to pursue that then you need to ask RankMath support how to Check if Main Category == X so we can use that as our display condition

This archived topic is closed to new replies.