Truncate WooCommerce Product Description With Toggle

I’ve said in previous blog posts that I enjoy reading requests and requirements from WooCommerce FaceBook groups and trying to implement them on my own as a learning experience and to gain more knowledge of WooCommerce development. In this post, we are going to truncate the long(er) product Description into a clickable ‘Read more’ type of option. Learn with me by reading below.

The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

Inset image by 200 Degrees from Pixabay 

Note: All of the examples in this post are part of a development/learning WooCommerce install using the default Storefront theme. PHP code is placed in the child theme’s function.php file.

Truncate Product Description

The idea for this article originated from a FaceBook post I saw in a WooCommerce group where the poster wanted to truncate the single product short description. For a better learning experience, I decided to target the (often) longer product Description.

Typically, a single product has a longer Description on the Single Product page after the product metadata:

We can target the woocommerce_after_single_product_summary hook with our own callback function in the add_action() function’s second parameter. Using jQuery in the wc_enqueue_js() function and targeting the .woocommerce-Tabs-panel–description CSS class, I truncate the product Description text after 45 characters and place a clickable ‘… Read more’ link.

add_action( 'woocommerce_after_single_product_summary', 'custom_woo_prod_description' );

function custom_woo_prod_description()
wc_enqueue_js( '
var num_chars = 45;
var replace_ellipses = " ... ";
var description_content = $(".woocommerce-Tabs-panel--description").html();
if (description_content.length > num_chars) {
var a = description_content.substr(0, num_chars);
var b = description_content.substr(num_chars - description_content.length);
var html = a + "<span class=\'truncated\'>" + replace_ellipses + "<a class=\'read-more\'>Read more</a></span><span class=\'truncated\' style=\'display:none\'>" + b + "</span>";
$(".read-more").click(function(e) {
$(".woocommerce-Tabs-panel--description .truncated").toggle();
' );

Now we can see the previous product Description has been replace by the ‘ … Read more’ link.

You can simply click on the ‘… Read more’ text link and see (toggle) the complete (remaining) product Description.

If you see any mistakes in the code or know of a better way to accomplish this functionality, please leave a comment below. Thank you 👍

Credit Resources

I was able to learn about and eventually write this blog post thanks to this fantastic resource that got me halfway there:

Thank you for reading this post. Please share it with someone else who would enjoy it as well.

Josh Otwell has a passion to grow as a PHP Developer, SQL expert, and technical blogger/writer.

Disclaimer: The majority of examples in this post, are performed in a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Like always, just because you can do something doesn’t mean you should. My opinions are my own.

More ways I can help

Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.

The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

📰 Get your brand, product, or service the attention it deserves with affordable classified ad placement in the OpenLampTech newsletter. I appreciate your support!

2 thoughts on “Truncate WooCommerce Product Description With Toggle

  1. Hi its not working for me. I am using elementor product single page from elementor theme builder

    • Hi Amanur. Unfortunately, it may be related to elemntor theme builder. But, I’m not sure as the example in the article are for default Storefront theme

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.