首页 > 解决方案 > 更改 Woocommerce 显示的购物车小计的小数位数

问题描述

我正在使用更改 Woocommerce 购物车总数中的小数位数答案代码以显示2 位小数的总数。该代码适用于购物车和结帐页面中显示的总数。

如果可能的话,我也想为小计做同样的事情。任何帮助表示赞赏。

在此处输入图像描述

标签: phpwordpresswoocommercedecimalcart

解决方案


要在购物车和结帐页面中使用 2 位小数格式化显示的小计金额,请使用以下命令:

// Displayed formatted cart subtotal in cart and checkout pages
add_filter( 'woocommerce_cart_subtotal', 'filter_cart_subtotal_html', 10, 3 );
function filter_cart_subtotal_html( $cart_subtotal, $compound, $cart ) {
    $decimals = 2; // <==  <==  Set the number of decimals to be displayed

    $args = ['decimals' => strval($decimals)]; // Initializing

    if ( $compound ) {
        $cart_subtotal = wc_price( $cart->get_cart_contents_total() + $cart->get_shipping_total() + $cart->get_taxes_total( false, false ), $args );

    } elseif ( $cart->display_prices_including_tax() ) {
        $cart_subtotal = wc_price( $cart->get_subtotal() + $cart->get_subtotal_tax(), $args );

        if ( $cart->get_subtotal_tax() > 0 && ! wc_prices_include_tax() ) {
            $cart_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
        }
    } else {
        $cart_subtotal = wc_price( $cart->get_subtotal(), $args );

        if ( $cart->get_subtotal_tax() > 0 && wc_prices_include_tax() ) {
            $cart_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
        }
    }

    return $cart_subtotal;
}

代码位于活动子主题(或活动主题)或插件文件的 functions.php 文件中。测试和工作。

相关:更改 Woocommerce 购物车总数中的小数位数


推荐阅读