我需要显示简单且可变的产品列表,其价格显示为“XX 美元起”。但我见过一些像下面这样的方法。
$min_price = $product->get_variation_price( 'min', true ); //I guess this only works for variable products
$min_price = $product->get_price_html(); //Will this guarantee to show the priceof the cheapest variation in a variable product?
$min_price = $product->get_price(); //How about this?
因此,这是确保我始终获得最低价格的产品的最佳方式,无论产品是简单的还是可变的。
最佳答案
The minimum price is exclusively for variable products (price range)
对于可变产品,仅为了获取您将使用的最低有效价格金额(未格式化):
$min_price = $product->get_variation_price( 'min' );
$min_price_for display = $product->get_variation_price( 'min', true ); // for display
对于其他产品类型(包括变体),要获取有效价格金额(未格式化),您将使用:
$price = $product->get_price();
要使所有产品获得格式化显示价格,您将使用:
$formatted_price = $product->get_price_html();
最后一个可以给出可变产品和促销产品的价格范围。
相关:WooCommerce variable products: keep only "min" price with a custom label
关于php - 在 WooCommerce 中获取简单和可变产品的最低价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57499149/