【问题标题】:Fetch specific data from Database for a product从数据库中获取产品的特定数据
【发布时间】:2017-03-15 20:35:14
【问题描述】:

我正在使用 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 时,不会“回显”任何内容。

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

谢谢

【问题讨论】:

    标签: php mysql wordpress woocommerce product


    【解决方案1】:

    如果您查看 class WC_Product,您会发现这个 WooCommerce 产品类包含预定义的方法,例如 get_regular_price(),但肯定不包含 get_bulkdiscount_discount_1()。您应该需要用新方法扩展这个 WC_Product 类,这是一个复杂的过程。

    要从您的 woocommerce 数据库中获取数据,其中键是 '_bulkdiscount_discount_1',您必须以这种方式使用带有定义产品 ID 的 WordPress 函数 get_post_meta()(如果 $_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>';
    

    【讨论】:

    • 太好了 - 非常感谢!真的很完美!
    猜你喜欢
    • 1970-01-01
    • 2020-04-29
    • 2019-05-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 2018-03-31
    相关资源
    最近更新 更多