2017-03-15 35 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