簡體   English   中英

如何使用變體ID獲得可變產品的價格?

[英]How to get the price of variable product using variation ID?

我有產品的版本ID。 有什么方法可以獲取特定版本ID的價格。

我嘗試了以下代碼。

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

如果您為該產品設置了銷售價格,則您的代碼將不會顯示正確的價格,因此您應使用_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則第一個代碼段將不起作用,而第二個代碼段將起作用。

希望這可以幫助!

你試過了嗎:

$variation_id = '12312';

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM