php - 以干净的方式正确覆盖 WooCommerce 函数 WC_Price()

标签 php wordpress woocommerce product 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, '<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 中使用 Hook 函数 以这种方式过滤钩子(Hook):

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 euros ):

<span class="price">
    <span class="woocommerce-Price-amount amount">
        <span class="custom-prc">42,03</span>
        "&nbsp;"
        <span class="woocommerce-Price-currencySymbol">€</span>
    </span>
</span>

关于php - 以干净的方式正确覆盖 WooCommerce 函数 WC_Price(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45108717/

相关文章:

php - Woocommerce购买须知?

wordpress - 向 WooCommerce 产品变体添加高级自定义字段

php - 如何使用 Eloquent Query Builder 对与同一张表的关系使用 whereHas?

javascript - 将csv文件上传到mysql数据库sql语法错误

php - 以下问题需要什么 SQL 命令?

css - 长 URL 不会留在 Bootstrap 列中

php - WooCommerce:结帐页面中的帐户密码字段

php - 我可以使用 PHP 提供自定义协议(protocol)吗?

json - Wordpress REST API _fields 和 _embed 参数不能同时工作

wordpress - WooCommerce Wordpress Yoast Hatom Rich Snippets 解决方案?