将折扣百分比添加到 Woocommerce 中的可变产品价格范围
Add the discounted percentage to variable product price range in Woocommerce
使用 Woocommerce,我已经使用以下代码从产品档案页面中成功删除了销售徽章和价格:
// Remove Sales Flash
add_filter('woocommerce_sale_flash', 'woo_custom_hide_sales_flash');
function woo_custom_hide_sales_flash()
{
return false;
}
// Remove prices on archives pages
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
所有产品都是可变产品,所有变体的价格都相同。而且实际上所有价格都是促销价。
我想在每个可变价格范围后添加折扣百分比,在单个产品页面。我尝试使用以下代码:
add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
$percentage = round( ( ( $product->regular_price – $product->sale_price ) /
$product->regular_price ) * 100 );
return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}
但我什么也没得到
我做错了什么,如何解决?
如有任何帮助,我们将不胜感激。
我一直在测试代码,因为您的目标是可变产品的销售价格范围,所以最好在 woocommerce_format_sale_price
过滤器挂钩中使用自定义挂钩函数,该挂钩位于 wc_format_sale_price()
函数中。
That will allow to display the saved discount percentage after the price range when all variations have the same prices. If variations prices are different, then this percentage will only appear on variations prices.
所以我重新访问了您的代码:
// Removing sale badge
add_filter('woocommerce_sale_flash', '__return_false');
// Removing archives prices
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
// Add the saved discounted percentage to variable products
add_filter('woocommerce_format_sale_price', 'add_sale_price_percentage', 20, 3 );
function add_sale_price_percentage( $price, $regular_price, $sale_price ){
// Strip html tags and currency (we keep only the float number)
$regular_price = strip_tags( $regular_price );
$regular_price = (float) preg_replace('/[^0-9.]+/', '', $regular_price);
$sale_price = strip_tags( $sale_price );
$sale_price = (float) preg_replace('/[^0-9.]+/', '', $sale_price);
// Percentage text and calculation
$percentage = __('Save', 'woocommerce') . ' ';
$percentage .= round( ( $regular_price - $sale_price ) / $regular_price * 100 );
// return on sale price range with "Save " and the discounted percentage
return $price . ' <span class="save-percent">' . $percentage . '%</span>';
}
代码进入您的活动子主题(活动主题)的 function.php 文件。
已测试并有效。
使用 Woocommerce,我已经使用以下代码从产品档案页面中成功删除了销售徽章和价格:
// Remove Sales Flash
add_filter('woocommerce_sale_flash', 'woo_custom_hide_sales_flash');
function woo_custom_hide_sales_flash()
{
return false;
}
// Remove prices on archives pages
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
所有产品都是可变产品,所有变体的价格都相同。而且实际上所有价格都是促销价。
我想在每个可变价格范围后添加折扣百分比,在单个产品页面。我尝试使用以下代码:
add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
$percentage = round( ( ( $product->regular_price – $product->sale_price ) /
$product->regular_price ) * 100 );
return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}
但我什么也没得到
我做错了什么,如何解决?
如有任何帮助,我们将不胜感激。
我一直在测试代码,因为您的目标是可变产品的销售价格范围,所以最好在 woocommerce_format_sale_price
过滤器挂钩中使用自定义挂钩函数,该挂钩位于 wc_format_sale_price()
函数中。
That will allow to display the saved discount percentage after the price range when all variations have the same prices. If variations prices are different, then this percentage will only appear on variations prices.
所以我重新访问了您的代码:
// Removing sale badge
add_filter('woocommerce_sale_flash', '__return_false');
// Removing archives prices
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
// Add the saved discounted percentage to variable products
add_filter('woocommerce_format_sale_price', 'add_sale_price_percentage', 20, 3 );
function add_sale_price_percentage( $price, $regular_price, $sale_price ){
// Strip html tags and currency (we keep only the float number)
$regular_price = strip_tags( $regular_price );
$regular_price = (float) preg_replace('/[^0-9.]+/', '', $regular_price);
$sale_price = strip_tags( $sale_price );
$sale_price = (float) preg_replace('/[^0-9.]+/', '', $sale_price);
// Percentage text and calculation
$percentage = __('Save', 'woocommerce') . ' ';
$percentage .= round( ( $regular_price - $sale_price ) / $regular_price * 100 );
// return on sale price range with "Save " and the discounted percentage
return $price . ' <span class="save-percent">' . $percentage . '%</span>';
}
代码进入您的活动子主题(活动主题)的 function.php 文件。
已测试并有效。