Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从数据库中获取产品的特定数据_Php_Mysql_Wordpress_Woocommerce_Product - Fatal编程技术网

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

Php 从数据库中获取产品的特定数据,php,mysql,wordpress,woocommerce,product,Php,Mysql,Wordpress,Woocommerce,Product,我正在使用WooCommerce,我需要从我的WooCommerce数据库中获取数据: 我知道,我可以通过以下代码获得商品的正常价格: $regPrice = $_product->get_regular_price(); 在数据库中,具有常规价格的字段如下所示:\u常规\u价格 我知道折扣在数据库中是这样的:\u bulkdiscount\u discount\u 1 所以我想,我可以像这样获得$折扣1: $discount1 = $_product->get_bulkdisco

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

我知道,我可以通过以下代码获得商品的正常价格:

$regPrice = $_product->get_regular_price();
在数据库中,具有常规价格的字段如下所示:\u常规\u价格

我知道折扣在数据库中是这样的:\u bulkdiscount\u discount\u 1

所以我想,我可以像这样获得$折扣1:

$discount1 = $_product->get_bulkdiscount_discount_1();
但不幸的是,这是行不通的。当我尝试使用以下代码回显$discount1时
echo“Discount One=$discount1

没有任何内容将被“回显”

我做错了什么
这里有谁能告诉我正确的方向吗

感谢

如果您查看,您将看到此WooCommerce产品类包括预定义的方法,例如,但肯定不是
get\u bulkdiscount\u discount\u 1()
。您应该需要使用新方法扩展这个WC_产品类,这是一个复杂的过程

要从woocommerce数据库中获取数据(键为
“\u bulkdiscount\u discount\u 1”
),您必须以这种方式使用具有定义产品ID的WordPress函数(如果
$\u product
是产品对象实例):

//确保获取产品ID(添加了与WC 3+的兼容性)
$product\U id=方法\U存在($product,'get\U id')$product->get_id():$product->id;
//回显“产品ID:”$产品标识。”

",; //从wp_PosteTa表中获取此产品ID(post ID)的批量折扣1 $discount1=get\u post\u meta($product\u id,''u bulkdiscount\u discount\u 1',true); //显示批量折扣1 回音“折扣一:”$折扣1。”

",;
太好了-非常感谢!工作真的很完美!
// 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>';