Woocommerce 中没有小数的自定义变量产品格式价格
Custom variable product formatted price without decimals in Woocommerce
我目前在激活的 child-theme 中的 functions.php 中使用此附加代码来删除产品使用变体时的价格范围(因此它只会显示价格 "From: X$" 不是 "From: X$ - Y$"):
add_filter( 'woocommerce_variable_sale_price_html',
'lw_variable_product_price', 10, 2 );
add_filter( 'woocommerce_variable_price_html',
'lw_variable_product_price', 10, 2 );
function lw_variable_product_price( $v_price, $v_product ) {
// Regular Price
$v_prices = array( $v_product->get_variation_price( 'min', true ),
$v_product->get_variation_price( 'max', true ) );
$v_price = $v_prices[0]!==$v_prices[1] ? sprintf(__('From: %1$s', 'woocommerce'),
wc_price( $v_prices[0] ) ) : wc_price( $v_prices[0] );
// Sale Price
$v_prices = array( $v_product->get_variation_regular_price( 'min', true ),
$v_product->get_variation_regular_price( 'max', true ) );
sort( $v_prices );
$v_saleprice = $v_prices[0]!==$v_prices[1] ? sprintf(__('From: %1$s','woocommerce')
, wc_price( $v_prices[0] ) ) : wc_price( $v_prices[0] );
if ( $v_price !== $v_saleprice ) {
$v_price = '<del>'.$v_saleprice.$v_product->get_price_suffix() . '</del> <ins>' .
$v_price . $v_product->get_price_suffix() . '</ins>';
}
return $v_price;
}
这里唯一的问题在标题中已经提到了。我需要在产品列表(默认商店页面)中显示不带小数的价格,而不是目前显示的那样:
我敢肯定,如果没有这个额外的代码,我正在使用它就没有这些零。
相信我,最后没有两个零看起来好多了。
要显示不带小数的价格您需要在 Woocommerce wc_price()
格式化函数中使用 'decimals'
参数…
因此,例如价格为 499.00
,您将向 wc_price()
添加参数 array('decimals' => 0)
:
echo wc_price( 499.00, array('decimals' => 0) );
它将输出格式化的 html 不带小数的价格。
如您所见,它使用 wc_price() 函数从任何格式化价格中删除小数点,因此在您的代码中例如:
$v_price = $v_prices[0]!==$v_prices[1] ? sprintf(__('From: %1$s', 'woocommerce'),
wc_price( $v_prices[0], array('decimals' => 0) ) ) : wc_price( $v_prices[0], array('decimals' => 0) );
For variable product custom prices, just as you want see the following answer:
You will have just to add array('decimals' => 0)
to wc_price()
function
我目前在激活的 child-theme 中的 functions.php 中使用此附加代码来删除产品使用变体时的价格范围(因此它只会显示价格 "From: X$" 不是 "From: X$ - Y$"):
add_filter( 'woocommerce_variable_sale_price_html',
'lw_variable_product_price', 10, 2 );
add_filter( 'woocommerce_variable_price_html',
'lw_variable_product_price', 10, 2 );
function lw_variable_product_price( $v_price, $v_product ) {
// Regular Price
$v_prices = array( $v_product->get_variation_price( 'min', true ),
$v_product->get_variation_price( 'max', true ) );
$v_price = $v_prices[0]!==$v_prices[1] ? sprintf(__('From: %1$s', 'woocommerce'),
wc_price( $v_prices[0] ) ) : wc_price( $v_prices[0] );
// Sale Price
$v_prices = array( $v_product->get_variation_regular_price( 'min', true ),
$v_product->get_variation_regular_price( 'max', true ) );
sort( $v_prices );
$v_saleprice = $v_prices[0]!==$v_prices[1] ? sprintf(__('From: %1$s','woocommerce')
, wc_price( $v_prices[0] ) ) : wc_price( $v_prices[0] );
if ( $v_price !== $v_saleprice ) {
$v_price = '<del>'.$v_saleprice.$v_product->get_price_suffix() . '</del> <ins>' .
$v_price . $v_product->get_price_suffix() . '</ins>';
}
return $v_price;
}
这里唯一的问题在标题中已经提到了。我需要在产品列表(默认商店页面)中显示不带小数的价格,而不是目前显示的那样:
我敢肯定,如果没有这个额外的代码,我正在使用它就没有这些零。
相信我,最后没有两个零看起来好多了。
要显示不带小数的价格您需要在 Woocommerce wc_price()
格式化函数中使用 'decimals'
参数…
因此,例如价格为 499.00
,您将向 wc_price()
添加参数 array('decimals' => 0)
:
echo wc_price( 499.00, array('decimals' => 0) );
它将输出格式化的 html 不带小数的价格。
如您所见,它使用 wc_price() 函数从任何格式化价格中删除小数点,因此在您的代码中例如:
$v_price = $v_prices[0]!==$v_prices[1] ? sprintf(__('From: %1$s', 'woocommerce'),
wc_price( $v_prices[0], array('decimals' => 0) ) ) : wc_price( $v_prices[0], array('decimals' => 0) );
For variable product custom prices, just as you want see the following answer:
You will have just to add
array('decimals' => 0)
towc_price()
function