2017-03-15 100 views
2

我使用WooCommerce,我需要从我woocommerce数据库中提取数据:获取来自数据库的具体数据产品

我知道,我可以得到一个项目的正常价格与此代码:

$regPrice = $_product->get_regular_price(); 

在数据库与正价场看起来是这样的:_regular_price

我知道折扣看起来像这样在数据库:_bulkdiscount_discount_1

所以我想,我可以得到这样的折扣1:

$discount1 = $_product->get_bulkdiscount_discount_1(); 

但不幸的是,这是行不通的。当我尝试使用这个代码echo "<p>Discount One = $discount1 </p>";来回显$ discount1时,什么都不会“回显”。

我做错了什么?
这里有没有任何Woocommerce Cracks谁能告诉我正确的方向?

感谢

回答

1

如果你看看class WC_Product你将看到这个WooCommerce产品类别包括预定义的方法,如get_regular_price(),但肯定不是get_bulkdiscount_discount_1()。您应该需要使用新方法扩展此WC_Product类,这是一个复杂的过程。

要从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>'; 
+0

太好了 - 非常感谢!作品非常完美! – Bosstone