从数据库中获取产品的特定数据

Fetch specific data from Database for a product

我正在使用 WooCommerce,我需要从我的 woocommerce 数据库中获取数据:

我知道,我可以使用此代码获取商品的正常价格:

$regPrice = $_product->get_regular_price();

在数据库中,正常价格的字段如下所示:_regular_price

我知道折扣在数据库中是这样的:_bulkdiscount_discount_1

所以我想,我可以这样获得 $discount1:

$discount1 = $_product->get_bulkdiscount_discount_1();

但不幸的是,这不起作用。当我尝试使用此代码 echo "<p>Discount One = $discount1 </p>"; 回显 $discount1 时,什么都不会 "echoed"。

我做错了什么?
这里有任何 Woocommerce 破解者可以向我提供正确方向的建议吗?

谢谢

如果你看class WC_Product you will see that this WooCommerce product class include predefined methods like get_regular_price(),但肯定不会get_bulkdiscount_discount_1()。您应该需要使用新方法扩展此 WC_Product class,这是一个复杂的过程。

要从您的 woocommerce 数据库中获取数据,键是 '_bulkdiscount_discount_1',您必须使用 WordPress 函数 get_post_meta() 和定义的产品 ID 这种方式 (如果 $_product是产品对象实例):

// Be sure to get the product ID (Added compatibility with WC 3+)
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// echo '<p>Product ID: ' . $product_id . '</p>';

// Get the bulk discount1 from wp_postmeta table for this product ID (post ID)
$discount1 = get_post_meta( $product_id, '_bulkdiscount_discount_1', true );
// Displaying the bulk discount 1
echo '<p>Discount One: ' . $discount1 . '</p>';