Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce - Get the product description by product_id

How can I get the product description or the product object using product ID.

$order = new WC_Order($order_id);

foreach ($order->get_items() as $item) {
    $product_description = get_the_product_description($item['product_id']); // is there any method can I use to fetch the product description?
}

The code above extends class WC_Payment_Gateway

Any help would be greatly appreciated.

like image 325
Vincent Panugaling Avatar asked Nov 04 '13 08:11

Vincent Panugaling


2 Answers

$order = new WC_Order($order_id);

foreach ($order->get_items() as $item)
{
    $product_description = get_post($item['product_id'])->post_content; // I used wordpress built-in functions to get the product object 
}
like image 135
Vincent Panugaling Avatar answered Oct 05 '22 23:10

Vincent Panugaling


If you are using WooCommerce 3.0 or above, then you can get description with the below code.

$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'];
}

Alternate way (recommended)

$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();
}

Hope this helps!

like image 35
Raunak Gupta Avatar answered Oct 05 '22 22:10

Raunak Gupta