How to Get Product Description & Display WooCommerce

WooCommerce Get Product Description & DisplayWhen 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.

Similar Articles