gpt4 book ai didi

php - 更改 Woocommerce 购物车总数中的小数位数

转载 作者:行者123 更新时间:2023-12-02 17:10:37 25 4
gpt4 key购买 nike

在 Woocommerce 中,我在 Woocommerce 常规设置中将小数位数设置为 7,因此我可以像这样显示产品价格 $0.0453321

我想知道我是否可以将购物车总数设置/四舍五入到只有 2 位小数(像这样的 $2.34 )?

最佳答案

正确的方法是仅更改购物车和结帐页面允许的小数位数:

add_filter( 'wc_get_price_decimals', 'change_prices_decimals', 20, 1 );
function change_prices_decimals( $decimals ){
if( is_cart() || is_checkout() )
$decimals = 2;
return $decimals;
}

代码在您的事件子主题(或事件主题)的 function.php 文件中。经过测试并有效。


要将显示的购物车总计设置为小数点后两位,请改用(仅适用于 Woocommerce 3.3+):

add_filter( 'woocommerce_cart_tax_totals', 'change_decimals_cart_tax_totals', 20, 2 );
function change_decimals_cart_tax_totals( $tax_totals, $cart ){
$decimals = array('decimals' => 2);

$taxes = $cart->get_taxes();
$tax_totals = array();

foreach ( $taxes as $key => $tax ) {
$code = WC_Tax::get_rate_code( $key );

if ( $code || $key === apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) ) {
if ( ! isset( $tax_totals[ $code ] ) ) {
$tax_totals[ $code ] = new stdClass();
$tax_totals[ $code ]->amount = 0;
}
$tax_totals[ $code ]->tax_rate_id = $key;
$tax_totals[ $code ]->is_compound = WC_Tax::is_compound( $key );
$tax_totals[ $code ]->label = WC_Tax::get_rate_label( $key );
$tax_totals[ $code ]->amount += wc_round_tax_total( $tax );
$tax_totals[ $code ]->formatted_amount = wc_price( wc_round_tax_total( $tax_totals[ $code ]->amount ), $decimals );
}
}

if ( apply_filters( 'woocommerce_cart_hide_zero_taxes', true ) ) {
$amounts = array_filter( wp_list_pluck( $tax_totals, 'amount' ) );
$tax_totals = array_intersect_key( $tax_totals, $amounts );
}
return $tax_totals;
}

add_filter( 'woocommerce_cart_totals_order_total_html', 'change_decimals_cart_totals_order_total_html', 20, 1 );
function change_decimals_cart_totals_order_total_html( $formatted_price ){
$decimals = array('decimals' => 2);

$value = '<strong>' . wc_price( WC()->cart->get_total('edit'), $decimals ) . '</strong> ';

// If prices are tax inclusive, show taxes here.
if ( wc_tax_enabled() && WC()->cart->display_prices_including_tax() ) {
$tax_string_array = array();
$cart_tax_totals = WC()->cart->get_tax_totals();

if ( get_option( 'woocommerce_tax_total_display' ) == 'itemized' ) {
foreach ( $cart_tax_totals as $code => $tax ) {
$tax_string_array[] = sprintf( '%s %s', $tax->formatted_amount, $tax->label );
}
} elseif ( ! empty( $cart_tax_totals ) ) {
$tax_string_array[] = sprintf( '%s %s', wc_price( WC()->cart->get_taxes_total( true, true ), $decimals ), WC()->countries->tax_or_vat() );
}

if ( ! empty( $tax_string_array ) ) {
$taxable_address = WC()->customer->get_taxable_address();
$estimated_text = WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping()
? sprintf( ' ' . __( 'estimated for %s', 'woocommerce' ), WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] )
: '';
$value .= '<small class="includes_tax">' . sprintf( __( '(includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) . $estimated_text ) . '</small>';
}
}
return $value;
}

代码在您的事件子主题(或事件主题)的 function.php 文件中。经过测试并有效。

You can't really round the prices in cart totals appart. If you do it with different hooks, you will get calculation errors. My code is just changing the number of decimals on displayed formatted prices and will not alter the calculations on real prices…

相关:Change number of decimals on Woocommerce displayed cart subtotal

关于php - 更改 Woocommerce 购物车总数中的小数位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49550127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com