栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在WC 3.0+的单一产品页面中显示接近销售价格的折扣百分比

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在WC 3.0+的单一产品页面中显示接近销售价格的折扣百分比

更新 -2019 (避免四舍五入的价格问题) -2017 (避免

NAN%
百分比值)

woocommerce_sale_price_html
在WooCommerce
3.0+中,该钩子已替换为另一个钩子,该钩子现在具有3个参数(但不再有该
$product
参数)。

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)

要舍入百分比,可以使用

round()
number_format()
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+版本。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/466158.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号