Leo*_*o C 5 php wordpress product woocommerce price
正确覆盖预先存在的 WooCommerce 功能的最佳方法是什么?在这种情况下我想修改这个wc_price()函数。我不需要对它做任何疯狂的事情,我实际上只需要<span>在价格周围添加一个 HTML 属性。
我知道代码如下:
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 );
}
Run Code Online (Sandbox Code Playgroud)
我想做的就是将其更改为:
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 );
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!谢谢!
要在价格周围添加自定义 html 标签<span class="custom-prc">0000</span>,您需要在formatted_woocommerce_price过滤器挂钩中使用挂钩函数,如下所示:
add_filter( \'formatted_woocommerce_price\', \'span_custom_prc\', 10, 5 );\nfunction span_custom_prc( $number_format, $price, $decimals, $decimal_separator, $thousand_separator){\n return \'<span class="custom-prc">\'.$number_format.\'</span>\';\n}\nRun Code Online (Sandbox Code Playgroud)\n\n代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。
\n\n该代码经过测试并可与 WooCommerce 3+ 配合使用
\n\n然后你将得到这个 html 输出(例如42,00 euros):
add_filter( \'formatted_woocommerce_price\', \'span_custom_prc\', 10, 5 );\nfunction span_custom_prc( $number_format, $price, $decimals, $decimal_separator, $thousand_separator){\n return \'<span class="custom-prc">\'.$number_format.\'</span>\';\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
11332 次 |
| 最近记录: |