drra6593 2018-03-29 07:16
浏览 246
已采纳

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

In Woocommerce, I've set the number of decimal to 7 on in Woocommerce general settings, so I can display the product price like this $0.0453321.

I'm wondering if I can set/round the cart total to only 2 decimals (something like this $2.34)?

  • 写回答

3条回答 默认 最新

  • duanjin9035 2018-03-29 08:23
    关注

    The correct way is to change just the number of allowed decimals for cart and checkout pages only:

    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;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.


    To set the dispayed cart gran total with 2 decimals use this instead (for Woocommerce 3.3+ only):

    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;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    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…

    Related: Change number of decimals on Woocommerce displayed cart subtotal

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启