php – 以干净的方式正确覆盖WooCommerce函数WC_Price()
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 以干净的方式正确覆盖WooCommerce函数WC_Price(),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3688字,纯文字阅读大概需要6分钟。
内容图文
![php – 以干净的方式正确覆盖WooCommerce函数WC_Price()](/upload/InfoBanner/zyjiaocheng/797/2f6036d41bd84fa8b97d1ce8582af24b.jpg)
正确覆盖预先存在的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, '<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol( $currency ) . '</span>', $price );
$return = '<span class="woocommerce-Price-amount amount">' . $formatted_price . '</span>';
if ( $ex_tax_label && wc_tax_enabled() ) {
$return .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
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, '<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol( $currency ) . '</span>', <span class="custom-prc"> . $price . </span> );
$return = '<span class="woocommerce-Price-amount amount">' . $formatted_price . '</span>';
if ( $ex_tax_label && wc_tax_enabled() ) {
$return .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
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 '<span class="custom-prc">'.$number_format.'</span>';
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中.
该代码经过测试,可与WooCommerce 3配合使用
然后你将得到这个html输出(例如42,00欧元):
<span class="price">
<span class="woocommerce-Price-amount amount">
<span class="custom-prc">42,03</span>
" "
<span class="woocommerce-Price-currencySymbol">€</span>
</span>
</span>
内容总结
以上是互联网集市为您收集整理的php – 以干净的方式正确覆盖WooCommerce函数WC_Price()全部内容,希望文章能够帮你解决php – 以干净的方式正确覆盖WooCommerce函数WC_Price()所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。