How to Display Product Description in WooCommerce Store (ok)
https://www.cloudways.com/blog/display-product-description-in-product-pages-on-woocommerce/
Last updated
https://www.cloudways.com/blog/display-product-description-in-product-pages-on-woocommerce/
Last updated
C:\xampp\htdocs\jp1\wp-content\themes\muum-child\functions.php
June 29, 2019 4 Min Read
WooCommerce comes packaged with archive pages and loops that make the WooCommerce product description display awesome. Yet here and there you may need to show more data on your main shop and other archive pages. This tutorial demonstrates how to show a WooCommerce short description of products to archive pages and display it below the title of the product. You can also add a minimum shipping requirement rule on products.
In your wp-content/plugins directory, create a PHP file with the name cloudways-product-shor-des.php
Open the file in your code editorial manager. At the top of the file, including this:
<?php
/*
* Plugin Name: Cloudways Display WooCommerce Product Short Description
* Description: Add WooCommerce product short description to the loop in product archive pages
* Version: 1.0
*/
This sets up the plugin and gives WordPress all that it needs to activate it.
Now go to the Plugins area in the WordPress admin area and find the plugin:
Now Activate the WooCommerce product description plugin.
At first, it won’t have any effect as you haven’t populated it. This is what the shop page looks like right at this point:
The short description for products in WooCommerce utilizes the excerpt that you’d find in normal posts. So to show it, you should simply show the excerpt for the post.
In your plugin file, add the code to get product description WooCommerce:
function cloudways_short_des_product() {
the_excerpt();
}
It’s as basic as that! Yet, now you have to hook your function to the right activity so that it’s output in the correct place in your archive pages.
Test your code, plugins and themes with ease.GET STARTED FREE
How about we investigate the file in WooCommerce that output the content of the loop on archive pages? This file is content-product.php, and you’ll see it in the templates folder in the WooCommerce plugin by using the WooCommerce product description hook. The file incorporates various action hooks, all of which are utilized by WooCommerce to output different content.
As we need to show our excerpt below the title of the product, the hook we have to utilize is woocommerce_after_shop_loop_item_title . As you can see from the content-product.php file, it’s now got two functions attached to it, woocommerce_template_loop_rating() and woocommerce_template_loop_price(), which have priorities of 5 and 10 separately. So we need to hook our function with a higher priority number, to ensure it fires after those. I’ll leave some leeway and use 40 as the priority.
Beneath your function, add this:
add_action( 'woocommerce_after_shop_loop_item_title', 'cloudways_short_des_product', 40 );
Now the complete WooCommerce description code becomes:
function cloudways_short_des_product() {
the_excerpt();
}
add_action( 'woocommerce_after_shop_loop_item_title', 'cloudways_short_des_product', 40 );
Now save your plugin file and refresh the shop page in your browser.
function get_ecommerce_excerpt(){
$excerpt = get_the_excerpt();
$excerpt = preg_replace(" ([.*?])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 100);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/s+/', ' ', $excerpt));
return $excerpt;
}
$excerpt = substr($excerpt, 0, 100); where 100 is character limit. you can manage content length by increasing and decreasing its value.
Further, replace the following function
the_excerpt();
by
echo get_ecommerce_excerpt();
in your plugin file having name cloudways-product-shor-des.php
Now save your plugin file and refresh the shop page in your browser.
By the following code you can get WooCommerce product description or the product object using product 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();
}
Since WooCommerce outputs the greater part of its content utilizing hooks, it’s appropriate to include more content by writing functions and connecting them to those hooks. The tutorial demonstrates how to add a short description of the product at product archives.
These descriptions are a bit long. To reduce the content length, add the following code in your functions.php located at theme folder.