2
正確覆蓋預先存在的WooCommerce函數的最佳方法是什麼?在這種情況下,我想修改wc_price()
函數。我不需要對它做任何瘋狂的事情,我實際上只需要在價格附近添加一個HTML <span>
屬性。正確覆蓋WooCommerce函數WC_Price()以清晰的方式
我知道代碼如下:
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);
}
任何幫助都將不勝感激!謝謝!
真棒!這正是我需要的。非常感謝你! –
非常感謝 – Hafsa