php - 使用短代码显示产品价格,包括 WooCommerce 中的可变产品
问题描述
我在 WooCommerce优秀答案中使用按产品 ID 显示产品价格和短代码,以通过短代码显示产品价格,但我需要一个还可以显示可变产品价格范围的解决方案。
价格范围应显示所有变体的最低可购买价格(常规或销售)到所有变体的最高可购买价格。它不需要显示销售价格和常规价格的两个范围,只需一个范围显示您实际可以购买的价格,无论是销售价格还是常规价格。
例如:
- 变化 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' );
也许可以从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;
}
编辑:我可以确认显示的价格已使用 Aelia Currency Converter 插件正确转换,因此无需进行任何更改。我已从问题中删除了该部分,并从上述代码中删除了货币代码,因为它是自动添加的。
我已经尝试了以下几乎可以正常工作的方法,它正确识别了可变产品类型并显示“从”和“到”字但不显示实际价格(因为我认为我需要从某个地方获取$from
and$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' );
推荐阅读
- kotlin - Kotlin 多平台期望/实际类,一些方法共享
- postgresql - 诸如 tablePlus、pgadmin DBvisualizer 之类的 Postgres 客户端工具不适用于带有 RDS 令牌的 .pgpass
- javascript - 如何减少开发中的内存使用
- mysql - 在 Laravel 中使用 MySql 进行考勤报告
- python-3.x - url 超过最大重试次数 由 NewConnectionError 导致无法建立新连接:[Errno -3] 名称解析临时失败
- node.js - 使用 hashmap 更新 xml
- java - 如何在 Java 中将当前日期转换为字符串格式?
- c++ - 使用 container.erase(iterator) 时出现分段错误
- html - 为什么我的桌子是垂直的而不是水平的?
- email - 成功迁移后的 Google Workspace 电子邮件问题