我用的是 在WooCommerce中用产品ID的简码显示产品价格。 很好的答案,通过一个短码来显示产品价格,但我需要一个解决方案,也可以显示可变产品的价格范围。
价格范围应该显示从所有变量的最低可购买价格(常规或销售)到所有变量的最高可购买价格。它不需要显示销售价格和普通价格的两个范围,只需要一个范围,显示你可以实际购买的价格,无论是销售还是普通价格。
比如说
以上应该显示的价格范围是3.00-8.00。
目前代码处理单品的方式不需要改变,我只需要把变量产品添加进去就可以了。
我正在使用的具体代码如下。
function custom_price_shortcode_callback( $atts ) {
$atts = shortcode_atts( array(
'id' => null,
), $atts, 'product_price' );
$html = '';
if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
// Get an instance of the WC_Product object
$product = wc_get_product( intval( $atts['id'] ) );
// Get the product prices
$price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
$regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price
// Formatting price settings (for the wc_price() function)
$args = array(
'ex_tax_label' => false,
'decimal_separator' => '.',
'thousand_separator' => ',',
'decimals' => 2,
'price_format' => '%1$s%2$s',
);
// Formatting html output
if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
$html = "<br/><del>" . wc_price( $regular_price, $args ) . "</del> " . wc_price( $sale_price, $args ); // Sale price is set
else
$html = "<br/>" . wc_price( $price, $args ); // No sale price set
}
return $html;
}
add_shortcode( 'product_price', 'custom_price_shortcode_callback' );
也许可以参考一下这个答案 WooCommerce可变产品价格前缀?
add_filter( 'woocommerce_format_price_range', 'format_price_range_prefix', 20, 3 );
function format_price_range_prefix( $price, $from, $to ) {
$price = sprintf( _x( 'From %1$s to %2$s %3$s', 'Price range: from-to', 'woocommerce' ), is_numeric( $from ) ? wc_price( $from ) : $from, is_numeric( $to ) ? wc_price( $to ) : $to, __('lv', 'woocommerce') );
return $price;
}
EDIT:我可以确认显示的价格是使用Aelia Currency Converter插件正确转换的,所以那里不需要任何改变。我已经从问题中删除了这部分,并从上面的代码中删除了货币代码,因为那是自动添加的。
我尝试了下面的方法,几乎是有效的,它正确地识别了可变的产品类型,并显示了 "from "和 "to "字样,但没有显示实际的价格(因为我认为我需要得到 "from "和 "to "字样。$from
和 $to
但不知道怎么用)。)
function custom_price_shortcode_callback( $atts ) {
$atts = shortcode_atts( array(
'id' => null,
), $atts, 'product_price' );
$html = '';
if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
$product = wc_get_product( intval( $atts['id'] ) );
if ( $product->is_type( 'variable' ) ){
$args = array(
'ex_tax_label' => false,
'decimal_separator' => '.',
'thousand_separator' => ',',
'decimals' => 2,
'price_format' => '%1$s%2$s',
);
$price = sprintf( _x( 'From %1$s to %2$s', 'Price range: from-to'), is_numeric( $from ) ? wc_price( $from, $args ) : $from, is_numeric( $to, $args ) ? wc_price( $to, $args ) : $to );
$html = "<br/>" . $price;
}
else{
// Get the product prices
$price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
$regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price
// Formatting price settings (for the wc_price() function)
$args = array(
'ex_tax_label' => false,
'decimal_separator' => '.',
'thousand_separator' => ',',
'decimals' => 2,
'price_format' => '%1$s%2$s',
);
// Formatting html output
if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
$html = "<br/><del>" . wc_price( $regular_price, $args ) . "</del> " . wc_price( $sale_price, $args ); // Sale price is set
else
$html = "<br/>" . wc_price( $price, $args ); // No sale price set
}
}
return $html;
}
add_shortcode( 'product_price', 'custom_price_shortcode_callback' );
如果我把可变产品部分包在格式价格范围部分中 那么可变产品部分就会完全停止工作。
if ( $product->is_type( 'variable' ) ){
$price = apply_filters( 'woocommerce_format_price_range', $price, $from, $to );
if ( !empty( $price ) ) {
$args = array(
'ex_tax_label' => false,
'decimal_separator' => '.',
'thousand_separator' => ',',
'decimals' => 2,
'price_format' => '%1$s%2$s',
);
$price = sprintf( _x( 'From %1$s to %2$s', 'Price range: from-to'), is_numeric( $from ) ? wc_price( $from, $args ) : $from, is_numeric( $to ) ? wc_price( $to, $args ) : $to );
$html = "<br/>" . $price;
}
}
这也不行。
if ( $product->is_type( 'variable' ) ){
$price = wc_format_price_range( $from, $to );
if ( !empty( $price ) ) {
$price = sprintf( _x( '%1$s – %2$s', 'Price range: from-to', 'woocommerce' ), is_numeric( $from ) ? wc_price( $from ) : $from, is_numeric( $to ) ? wc_price( $to ) : $to );
$html = "<br/>" . $price;
}
}
我正在使用下面的解决方案,这并不完全是我所设定的,因为它实际上没有显示可变产品的价格范围,而是显示一个 "来自 "的消息(可以更改),然后是一个单一的价格。
然而,现在它确实可以通过短码为标准产品和可变产品工作,所以我需要什么。
function custom_price_shortcode_callback( $atts ) {
$atts = shortcode_atts( array(
'id' => null,
), $atts, 'product_price' );
$html = '';
if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
$product = wc_get_product( intval( $atts['id'] ) );
if ( $product->is_type( 'variable' ) ){
$prefix = sprintf('%s ', __('From','woocommerce'));
$min_price_regular = $product->get_variation_regular_price( 'min', true );
$min_price_sale = $product->get_variation_sale_price( 'min', true );
$max_price = $product->get_variation_price( 'max', true );
$min_price = $product->get_variation_price( 'min', true );
$price = ( $min_price_sale == $min_price_regular ) ? wc_price( $min_price_regular ) : wc_price( $min_price_sale );
return
( $min_price == $max_price ) ? sprintf('<br/>' . $price) : $html = sprintf('<br/><span class="from-price">%s%s</span>', $prefix, $price);
}
else{
// Get the product prices
$price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
$regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price
// Formatting price settings (for the wc_price() function)
$args = array(
'ex_tax_label' => false,
'decimal_separator' => '.',
'thousand_separator' => ',',
'decimals' => 2,
'price_format' => '%1$s%2$s',
);
// Formatting html output
if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
$html = "<br/><del>" . wc_price( $regular_price, $args ) . "</del> " . wc_price( $sale_price, $args ); // Sale price is set
else
$html = "<br/>" . wc_price( $price, $args ); // No sale price set
}
}
return $html;
}
add_shortcode( 'product_price', 'custom_price_shortcode_callback' );
使用 Booster for woocommerce Plugin。并将此[wcj_product_price]短码放在你想要的任何地方。它将显示简单或可变的产品价格与货币符号。而这个插件是非常有帮助的woommerce。