gpt4 book ai didi

php - 如何使用变体 ID 获取可变产品的价格?

转载 作者:行者123 更新时间:2023-12-05 08:57:42 26 4
gpt4 key购买 nike

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

我尝试了以下代码。

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

最佳答案

You code will not display the correct price if you have set a sale price for that product so you should use _price key for that.

这是为您锻炼的代码。

$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,那么第一个代码片段将不会可以,但第二个可以。

希望这对您有所帮助!

关于php - 如何使用变体 ID 获取可变产品的价格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30187958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com