10

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

任何帮助将不胜感激。

4

2 回答 2

20
$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 
}
于 2013-11-14T06:46:32.597 回答
11

如果您使用的是 WooCommerce3.0或更高版本,则可以使用以下代码获取描述。

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

希望这可以帮助!

于 2017-07-02T07:09:41.107 回答