Anonymous
Правильное переопределение функции WooCommerce WC_Price() чистым способом
Сообщение
Anonymous » 20 май 2024, 12:16
Как лучше всего правильно переопределить уже существующую функцию WooCommerce? В этом случае я хочу изменить функцию wc_price(). Мне не нужно делать с этим ничего сумасшедшего, мне буквально нужно просто добавить атрибут 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, '' . 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 );
}
Будем очень благодарны за любую помощь! Спасибо!
Подробнее здесь:
https://stackoverflow.com/questions/451 ... -clean-way
1716196564
Anonymous
Как лучше всего правильно переопределить уже существующую функцию WooCommerce? В этом случае я хочу изменить функцию wc_price(). Мне не нужно делать с этим ничего сумасшедшего, мне буквально нужно просто добавить атрибут HTML вокруг цены. Я знаю, код следующий: [code]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 ); } [/code] Все, что я хочу сделать, это изменить его на: [code]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 ); } [/code] Будем очень благодарны за любую помощь! Спасибо! Подробнее здесь: [url]https://stackoverflow.com/questions/45108717/overriding-properly-woocommerce-function-wc-price-in-a-clean-way[/url]