【问题标题】:How to get the price of variable product using variation ID?如何使用变体 ID 获取可变产品的价格?
【发布时间】:2015-07-23 03:43:14
【问题描述】:

我有产品的变体 ID。有什么方法可以获取特定变体 ID 的价格。

我尝试了以下代码。

$variation_id = 12312;
$price = get_post_meta($variation_id, '_regular_price', true);

【问题讨论】:

    标签: php wordpress woocommerce price variations


    【解决方案1】:

    如果您设置了销售,您的代码将不会显示正确的价格 该产品的价格,因此您应该为此使用_price 密钥。

    这是适合你锻炼的代码。

    $variation_id = '12312';
    $price = get_post_meta($variation_id, '_price', true);
    

    $variation_id = '12312';
    $variable_product = wc_get_product($variation_id);
    //$regular_price = $variable_product->get_regular_price();
    //$sale_price = $variable_product->get_sale_price();
    $price = $variable_product->get_price();
    

    请注意:您可以使用上述任何一种方法,但我建议您使用最后一种,因为如果 WooCommerce 更改 metakey,那么第一个代码 sn-p 将不起作用,但第二个就可以了。

    希望这会有所帮助!

    【讨论】:

    • 将代码的最后一行更改为 $price = $variable_product->get_price(); 会不会更加面向未来?
    【解决方案2】:

    你试过了吗:

    $variation_id = '12312';
    
    $price = get_post_meta($variation_id, '_regular_price', true);
    

    【讨论】:

    • 当时这对我有用,不知道为什么有人会否决这个评论。
    猜你喜欢
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 2015-12-31
    • 2023-03-14
    • 2020-01-27
    相关资源
    最近更新 更多