WooCommerce - 按product_id获取产品说明

时间:2013-11-04 08:22:25

标签: php wordpress woocommerce product orders

如何使用产品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?
}

上面的代码扩展了类WC_Payment_Gateway

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:15)

$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 
}

答案 1 :(得分:5)

如果您使用的是WooCommerce 3.0或更高版本,则可以使用以下代码获取 description

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

替代方式(推荐

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

希望这有帮助!