Php 显示Woocommerce产品的折扣价格和百分比
下图显示了折扣价格和百分比 我没有找到具有此功能的自定义代码搜索 我使用以下代码显示折扣价格,但价格未格式化(缺少货币符号和小数):Php 显示Woocommerce产品的折扣价格和百分比,php,wordpress,woocommerce,product,price,Php,Wordpress,Woocommerce,Product,Price,下图显示了折扣价格和百分比 我没有找到具有此功能的自定义代码搜索 我使用以下代码显示折扣价格,但价格未格式化(缺少货币符号和小数): add_filter('woocommerce_get_price_html','modify_woocommerce_get_price_html',10,2); 函数modify\u woocommerce\u get\u price\u html($price$product){ 如果($product->is_on_sale()&&!is_admin())
add_filter('woocommerce_get_price_html','modify_woocommerce_get_price_html',10,2);
函数modify\u woocommerce\u get\u price\u html($price$product){
如果($product->is_on_sale()&&!is_admin())
返回$price.sprintf(“保存:%s
,“woocommerce”),$product->regular\u price-$product->sale\u price);
其他的
返回$price;
}
如何显示正确格式的折扣价格?
如何显示折扣百分比
任何帮助都已应用。您的代码有点过时,因为无法直接访问woocommerce版本3 as产品对象属性。相反,您应该使用可用的
WC\u产品
方法
要格式化价格,您将使用wc\u 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;
}
add_filter('woocommerce_get_price_html','change_display_sale_price_html',10,2);
功能更改\u显示\u销售\u价格\u html($price,$product){
//仅在前端销售产品,不包括可变产品的最低/最高价格
如果($product->is_on_sale()&&!is_admin()&&!$product->is_type('variable')){
//获取产品价格
$regular_price=(float)$product->get_regular_price();//常规价格
$sale_price=(float)$product->get_price();//有效价格(销售时的“销售价格”)
//“节省价格”计算和格式化
$saving_price=wc_price($regular_price-$sale_price);
//附加到格式化的html价格
$price.=sprintf($saving\u('p class=“saved sale”>保存:%s,'woocommerce'),$saving\u price);
}
返回$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;
}
add_filter('woocommerce_get_price_html','change_display_sale_price_html',10,2);
功能更改\u显示\u销售\u价格\u html($price,$product){
//仅在前端销售产品,不包括可变产品的最低/最高价格
如果($product->is_on_sale()&&!is_admin()&&!$product->is_type('variable')){
//获取产品价格
$regular_price=(float)$product->get_regular_price();//常规价格
$sale_price=(float)$product->get_price();//有效价格(销售时的“销售价格”)
//“保存百分比”计算和格式设置
$precision=1;//最大小数位数
$saving_percentage=round(100-($sale_price/$regular_price*100),1)。“%”;
//附加到格式化的html价格
$price.=sprintf(“保存:%s
,“woocommerce”),$saving\u百分比);
}
返回$price;
}
3两者(折扣价格和百分比):
add_filter('woocommerce_get_price_html','change_display_sale_price_html',10,2);
功能更改\u显示\u销售\u价格\u html($price,$product){
//仅在前端销售产品,不包括可变产品的最低/最高价格
如果($product->is_on_sale()&&!is_admin()&&!$product->is_type('variable')){
//获取产品价格
$regular_price=(float)$product->get_regular_price();//常规价格
$sale_price=(float)$product->get_price();//有效价格(销售时的“销售价格”)
//“节省价格”计算和格式化
$saving_price=wc_price($regular_price-$sale_price);
//“保存百分比”计算和格式设置
$precision=1;//最大小数位数
$saving_percentage=round(100-($sale_price/$regular_price*100),1)。“%”;
//附加到格式化的html价格
$price.=sprintf(“保存:%s(%s)”,“woocommerce”),$saving\u price,$saving\u percentage);
}
返回$price;
}
代码进入活动子主题(或活动主题)的function.php文件
经过测试,效果良好。哇,谢谢你按照我想要的内容更正了这篇文章。谢谢你给我的完美答案。没有什么比这更珍贵的了,我解决了这个问题。非常感谢,祝你好运。
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;
}
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;
}