php框架 wc if_php – 以干净的方式正确覆盖WooCommerce函数WC_Price()

正确覆盖预先存在的WooCommerce功能的最佳方法是什么?在这种情况下,我想修改wc_price()函数.我不需要做任何疯狂的事情,我只需要添加一个HTML< span>围绕价格的属性.

我知道代码如下:

function wc_price( $price, $args = array() ) {

extract( apply_filters( 'wc_price_args', wp_parse_args( $args, array(

'ex_tax_label' => false,

'currency' => '',

'decimal_separator' => wc_get_price_decimal_separator(),

'thousand_separator' => wc_get_price_thousand_separator(),

'decimals' => wc_get_price_decimals(),

'price_format' => get_woocommerce_price_format(),

) ) ) );

$negative = $price < 0;

$price = apply_filters( 'raw_woocommerce_price', floatval( $negative ? $price * -1 : $price ) );

$price = apply_filters( 'formatted_woocommerce_price', number_format( $price, $decimals, $decimal_separator, $thousand_separator ), $price, $decimals, $decimal_separator, $thousand_separator );

if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $decimals > 0 ) {

$price = wc_trim_zeros( $price );

}

$formatted_price = ( $negative ? '-' : '' ) . sprintf( $price_format, '' . get_woocommerce_currency_symbol( $currency ) . '', $price );

$return = '' . $formatted_price . '';

if ( $ex_tax_label && wc_tax_enabled() ) {

$return .= ' ' . WC()->countries->ex_tax_or_vat() . '';

}

return apply_filters( 'wc_price', $return, $price, $args );

}

我想要做的就是将其改为:

function wc_price( $price, $args = array() ) {

extract( apply_filters( 'wc_price_args', wp_parse_args( $args, array(

'ex_tax_label' => false,

'currency' => '',

'decimal_separator' => wc_get_price_decimal_separator(),

'thousand_separator' => wc_get_price_thousand_separator(),

'decimals' => wc_get_price_decimals(),

'price_format' => get_woocommerce_price_format(),

) ) ) );

$negative = $price < 0;

$price = apply_filters( 'raw_woocommerce_price', floatval( $negative ? $price * -1 : $price ) );

$price = apply_filters( 'formatted_woocommerce_price', number_format( $price, $decimals, $decimal_separator, $thousand_separator ), $price, $decimals, $decimal_separator, $thousand_separator );

if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $decimals > 0 ) {

$price = wc_trim_zeros( $price );

}

$formatted_price = ( $negative ? '-' : '' ) . sprintf( $price_format, '' . get_woocommerce_currency_symbol( $currency ) . '', . $price . );

$return = '' . $formatted_price . '';

if ( $ex_tax_label && wc_tax_enabled() ) {

$return .= ' ' . WC()->countries->ex_tax_or_vat() . '';

}

return apply_filters( 'wc_price', $return, $price, $args );

}

任何帮助都将非常感谢!谢谢!

解决方法:

要添加自定义html标记< span class =“custom-prc”> 0000< / span>在价格附近,您将需要在formatted_woocommerce_price过滤器钩子中使用钩子函数:

add_filter( 'formatted_woocommerce_price', 'span_custom_prc', 10, 5 );

function span_custom_prc( $number_format, $price, $decimals, $decimal_separator, $thousand_separator){

return ''.$number_format.'';

}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中.

该代码经过测试,可与WooCommerce 3配合使用

然后你将得到这个html输出(例如42,00欧元):

42,03

" "

标签:wordpress,php,woocommerce,product,price

来源: https://codeday.me/bug/20190622/1264618.html

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下 4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值