![](/img/trans.png)
[英]Display the default discounted price and percentage on Woocommerce products
[英]Display the discounted price and percentage on Woocommerce products
在下图中,它显示了折扣价和百分比
我没有发现具有此功能的自定义代码搜索。
我使用下面的代码来显示折扣价,但价格没有格式化(缺少货币符号和小数):
add_filter( 'woocommerce_get_price_html', 'modify_woocommerce_get_price_html', 10, 2 );
function modify_woocommerce_get_price_html( $price, $product ) {
if( $product->is_on_sale() && ! is_admin() )
return $price . sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $product->regular_price - $product->sale_price );
else
return $price;
}
如何显示正确格式的折扣价? 我怎样才能显示折扣百分比?
任何帮助表示赞赏。
由于无法直接访问产品对象属性,因此您的代码有点过时,因为 woocommerce 版本 3。 相反,您应该使用可用的WC_Product
方法。
要格式化价格,您将使用wc_price()
专用格式化函数。
现在您可以拥有(3 种可能性) :
1) 节省价格:
add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
// Only on sale products on frontend and excluding min/max price on variable products
if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
// Get product prices
$regular_price = (float) $product->get_regular_price(); // Regular price
$sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)
// "Saving price" calculation and formatting
$saving_price = wc_price( $regular_price - $sale_price );
// Append to the formated html price
$price .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_price );
}
return $price;
}
2) 节省百分比:
add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
// Only on sale products on frontend and excluding min/max price on variable products
if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
// Get product prices
$regular_price = (float) $product->get_regular_price(); // Regular price
$sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)
// "Saving Percentage" calculation and formatting
$precision = 1; // Max number of decimals
$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';
// Append to the formated html price
$price .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage );
}
return $price;
}
3 两者(折扣价和百分比) :
add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
// Only on sale products on frontend and excluding min/max price on variable products
if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
// Get product prices
$regular_price = (float) $product->get_regular_price(); // Regular price
$sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)
// "Saving price" calculation and formatting
$saving_price = wc_price( $regular_price - $sale_price );
// "Saving Percentage" calculation and formatting
$precision = 1; // Max number of decimals
$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';
// Append to the formated html price
$price .= sprintf( __('<p class="saved-sale">Save: %s <em>(%s)</em></p>', 'woocommerce' ), $saving_price, $saving_percentage );
}
return $price;
}
代码位于活动子主题(或活动主题)的 function.php 文件中。
测试和工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.