【问题标题】:How to programmatically grab the product description in WooCommerce?如何以编程方式获取 WooCommerce 中的产品描述?
【发布时间】:2019-05-07 12:13:53
【问题描述】:

我看到了,WooCommerce - Get the product description by product_id

但我希望有一种更短的方法来通过 ID 获取产品并获取描述?有什么想法吗?

我正在使用 WooCommerce 3.0 及更高版本。

【问题讨论】:

  • “更短”的方式是什么意思?那个代码很短。您是在询问如何从订单中的产品中获取描述,还是只想通过 ID 获取产品并获取描述?请向我们展示您的尝试。

标签: php wordpress woocommerce product


【解决方案1】:

在您的问题中,我们不知道您所说的“GRAB”是什么意思……可能是如何设置(或如何获取)产品描述。


设置产品描述有多种方法。

1) 从产品 ID 使用 Wordpress wp_update_post()

  wp_update_post( array('ID' => $product_id, 'post_content' => 'This is the <strong>product description</strong> content.') );

2) 从WC_Product 对象使用set_description() 方法

// Get the WC_Product Object instance (optional if needed)
$product = wc_get_product( $product_id );

// Set the description
$product->set_description('This is the <strong>product description</strong> content.');

$product->save(); // Save product data

GET产品描述有多种方法(如in this thread 所述)。

1) 从产品 ID,使用 Wordpress get_post() 函数:

 $product_description = get_post( $product_id )->post_content;

2) 从WC_Product 对象使用get_description() 方法

// Get the WC_Product Object instance (optional if needed)
$product = wc_get_product( $product_id );

// Get the product description
$description = $product->get_description();

【讨论】:

    猜你喜欢
    • 2016-10-19
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多