When you are customizing the WooCommerce theme like I covered in the Storefront theme customization you may want to get product descriptions to display in different places like the shop page.
If you are looking for a quick way to get a product description this tutorial will guide you.
WooCommerce Get Product Description & Display
There are several ways you can get the product description but the most common way is using the product ID.
WooCommerce Get Product Description by product_id
This approach allows you to get the product description across all the pages in WooCommerce. The following is a sample code to get the product description using ID:
$order = wc_get_order($order_id); foreach ($order->get_items() as $item) { $product_id = $item['product_id']; $product_details = $product->get_data(); $product_full_description = $product_details['description']; $product_short_description = $product_details['short_description']; } //By using wc_get_product, get_description() and get_short_description() $order = wc_get_order($order_id); foreach ($order->get_items() as $item) { $product_id = $item['product_id']; $product_instance = wc_get_product($product_id); $product_full_description = $product_instance->get_description(); $product_short_description = $product_instance->get_short_description(); }
Function to Get Short Description WooCommerce
WooCommerce products are basically Custom Post Types, you can get short descriptions using the excerpt() function which is a default WordPress function that displays
function njengah_short_des_product() { the_excerpt(); }
This function can be used to display the WooCommerce short description on the shop page using the following hook :
function njengah_short_des_product() { the_excerpt(); } add_action( 'woocommerce_after_shop_loop_item_title', 'njengah_short_des_product', 40 );
Conclusion
In this post, we have highlighted the best way to get product descriptions in WooCommerce and how to display the product description using the excerpt function. You can add these code snippets to the functions.php file of your theme or in your plugin’s file.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Hide Product Description Heading WooCommerce
- How to Hide WooCommerce Marketing Hub Admin Option
- How to Clear Cart on Logout In WooCommerce
- How to Add Description after Price in WooCommerce
- Move Price WooCommerce Storefront Hooks
- How to Hide Out-of-Stock Visibility In WooCommerce
- WooCommerce Mobile Checkout Optimization Quick Guide
- How to Get Order Details After Checkout In WooCommerce
- How to Hide Tax Label In WooCommerce
- WooCommerce Checkout Optimization Quick Guide
- How to Hide Errors From Visitors WooCommerce
- How to Customize Storefront Theme Using Customizer
- How to Display Category Names in WordPress Using a Practical Example
- How to Hide, Change or Remove WooCommerce On Sale Badge
- How to Move Description Under Image in WooCommerce
- How to Get Order ID on Checkout Page WooCommerce
- How to Hide Price If Zero In WooCommerce
- How to Remove Product Category Title WooCommerce