使用短代码显示产品价格,包括 WooCommerce 中的可变产品
Display product prices with short code including variable products in WooCommerce
我正在使用 优秀的答案,通过短代码显示产品价格,但我需要一个也可以显示可变产品价格范围的解决方案。
价格范围应显示所有变体的最低可购买价格(常规或促销)到所有变体的最高可购买价格。它不需要显示促销价和常规价格的两个范围,只需一个范围显示您实际可以购买的价格,无论是促销价还是常规价。
例如:
- 变体 1 - 价格 4.00
- 变体 2 - 价格 4.00 促销价 3.00
- 变体 3 - 价格 6.00 促销价 5.00
- 变体 4 - 价格 8.00
以上应显示 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' );
也许可以合并来自 的答案?
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;
}
编辑:我可以确认显示的价格是使用 Aelia 货币转换器插件正确转换的,因此不需要在那里进行任何更改。我已经从问题中删除了那部分,并从上面的代码中删除了自动添加的货币代码。
我尝试了以下几乎有效的方法,它正确识别了可变产品类型并显示了 '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;
}
}
使用 Booster for woocommerce 插件。并将这个 [wcj_product_price] 简码放在任何你想要的地方。它将显示带有货币符号的简单或可变产品价格。而且这个插件对woommerce很有帮助。
我正在使用以下解决方案,这与我的初衷并不完全相同,因为它没有显示可变产品的价格范围,而是实际显示 'From' 消息(可以更改)然后是单价。
但是它现在可以通过短代码对标准产品和可变产品起作用,我需要的也是如此。
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' );
我正在使用
价格范围应显示所有变体的最低可购买价格(常规或促销)到所有变体的最高可购买价格。它不需要显示促销价和常规价格的两个范围,只需一个范围显示您实际可以购买的价格,无论是促销价还是常规价。
例如:
- 变体 1 - 价格 4.00
- 变体 2 - 价格 4.00 促销价 3.00
- 变体 3 - 价格 6.00 促销价 5.00
- 变体 4 - 价格 8.00
以上应显示 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' );
也许可以合并来自
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;
}
编辑:我可以确认显示的价格是使用 Aelia 货币转换器插件正确转换的,因此不需要在那里进行任何更改。我已经从问题中删除了那部分,并从上面的代码中删除了自动添加的货币代码。
我尝试了以下几乎有效的方法,它正确识别了可变产品类型并显示了 '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;
}
}
使用 Booster for woocommerce 插件。并将这个 [wcj_product_price] 简码放在任何你想要的地方。它将显示带有货币符号的简单或可变产品价格。而且这个插件对woommerce很有帮助。
我正在使用以下解决方案,这与我的初衷并不完全相同,因为它没有显示可变产品的价格范围,而是实际显示 'From' 消息(可以更改)然后是单价。
但是它现在可以通过短代码对标准产品和可变产品起作用,我需要的也是如此。
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' );