在 WC 3.0+ 的单品页面中显示接近销售价格的折扣百分比 [英] Display the discounted percentage near sale price in Single product pages for WC 3.0+

查看:25
本文介绍了在 WC 3.0+ 的单品页面中显示接近销售价格的折扣百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主题的 function.php 中有这个代码来显示价格后的百分比,它在 WooCommerce v2.6.14 中运行良好.

I had this code in function.php of my theme to display the percentage after price and it was working fine in WooCommerce v2.6.14.

但是此代码段在 WooCommerce 3.0+ 版上不再适用.

But this snippet doesn't work anymore on WooCommerce version 3.0+.

我该如何解决?

代码如下:

// Add save percent next to sale item prices.
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 . '%' );
}

推荐答案

更新 - 2019 (避免四舍五入价格问题) - 2017 (避免 NAN% 百分比值)

Updated - 2019 (avoid rounding price issue) - 2017 (avoid NAN% percentage value)

woocommerce_sale_price_html 钩子已被 WooCommerce 3.0+ 中的不同钩子取代,现在有 3 个参数(但不是 $product代码> 参数不再).

woocommerce_sale_price_html hook has been replaced by a different hook in WooCommerce 3.0+, that has now 3 arguments (but not the $product argument anymore).

add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
    // Getting the clean numeric prices (without html and currency)
    $_reg_price = floatval( strip_tags($regular_price) );
    $_sale_price = floatval( strip_tags($sale_price) );

    // Percentage calculation and text
    $percentage = round( ( $_reg_price - $_sale_price ) / $_reg_price * 100 ).'%';
    $percentage_txt = ' ' . __(' Save ', 'woocommerce' ) . $percentage;

    $formatted_regular_price = is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price;
    $formatted_sale_price    = is_numeric( $sale_price )    ? wc_price( $sale_price )    : $sale_price;

    echo '<del>' . $formatted_regular_price . '</del> <ins>' . $formatted_sale_price . $percentage_txt . '</ins>';
}

此代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中.
代码经过测试并有效.对于 WooCommerce 3.0+ 版 (感谢 @Mikebcn 和 @AsifRao)

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and works. For WooCommerce version 3.0+ (thanks to @Mikebcn and @AsifRao)

要舍入百分比,您可以使用 round()number_format()number_format_i18n():

For rounding the percentage you can use round(), number_format() or number_format_i18n():

$percentage = number_format_i18n( ( $_reg_price - $_sale_price ) / $_reg_price * 100, 0 ).'%';

$percentage = number_format( ( $_reg_price - $_sale_price ) / $_reg_price * 100, 0 ).'%';

<小时>

原始答案代码:这是功能相似的代码:

// Only for WooCommerce version 3.0+
add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
    $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
    $percentage_txt = ' ' . __(' Save ', 'woocommerce' ) . $percentage;
    $price = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del> <ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) . $percentage_txt : $sale_price . $percentage_txt ) . '</ins>';
    return $price;
}

此代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中.
代码经过测试并有效.对于 WooCommerce 版本 3.0+.

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and works. For WooCommerce version 3.0+.

这篇关于在 WC 3.0+ 的单品页面中显示接近销售价格的折扣百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆