【问题标题】:WooCommerce - Get the product description by product_idWooCommerce - 通过 product_id 获取产品描述
【发布时间】:2013-11-14 20:19:34
【问题描述】:

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

任何帮助将不胜感激。

【问题讨论】:

  • 提示:如果您的描述写在帖子内容中,请使用文森特的答案;如果您的描述写在 WooCommerce 的产品描述字段中,请使用 Raunak 的答案。

标签: php wordpress woocommerce product orders


【解决方案1】:
$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 
}

【讨论】:

    【解决方案2】:

    如果您使用的是 WooCommerce 3.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();
    }
    

    希望这会有所帮助!

    【讨论】:

    • 我想,少了一行 $product = new WC_Product($product_id);我还建议使用特定方法来确保执行钩子: $product_full_description = $product->get_description(); $product_short_description = $product->get_short_description();
    猜你喜欢
    • 2016-10-19
    • 2018-07-27
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多