我有一个产品列表,并且在可变产品中显示价格范围的问题。
您可以看到价格是否正常,一切都可以。但是,如果产品的价格为0€
,该产品的价格可变。好吧,显示价格的代码是:
<?php
if (isset($ebookstore_theme_option['woo-list-price']) &&
$ebookstore_theme_option['woo-list-price'] == 'enable') {
$ebookstore_woo .= '<strong class="amount">'.esc_attr($currency).''.$price_sale.'</strong>';
}
关于如何显示可变范围价格的任何想法?
对于WooCommerce中的可变产品,您需要:
- 目标变量产品类型
- 获得最低价格和最高价格。
因此,您应该需要获取产品对象的实例。通常,您可以使用:
global $product;
如果它不起作用,您将使用Intead:
global $post;
$product = wc_get_product( $post->ID );
现在从这一点开始,您将使用以下内容:
<?php
global $product;
if ( isset( $ebookstore_theme_option['woo-list-price'] ) && $ebookstore_theme_option['woo-list-price'] == 'enable' ){
// For all product types that are not "variable products"
if( ! $product->is_type('variable') ){
$ebookstore_woo .= '<strong class="amount">'.esc_attr($currency).''.$price_sale.'</strong>';
}
// For variable products
else {
$min_price = $product->get_variation_price( 'min' );
$max_price = $product->get_variation_price( 'max' );
$ebookstore_woo .= '<strong class="amount">'.esc_attr($currency).''.$min_price.' - '.esc_attr($currency).''.$max_price.'</strong>';
}
}
您可以看到我正在使用WC_Product_Variable
方法
可能是您不知道的,但是WooCommerce具有几种格式的价格功能:
-
wc_price( $price );
-
wc_format_price_range( $from, $to );
-
wc_format_sale_price( $regular_price, $sale_price );
他们都将包括货币