代码之家  ›  专栏  ›  技术社区  ›  Miguel Angel Martinez

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

  •  2
  • Miguel Angel Martinez  · 技术社区  · 7 年前

    在Woocommerce中,我在Woocommerce常规设置中将十进制数设置为7,以便显示 产品价格 像这样 $0.0453321

    我想知道我是否能在 购物车总计 只有2位小数 $2.34 )?

    3 回复  |  直到 7 年前
        1
  •  6
  •   LoicTheAztec    5 年前

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

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

    代码进入功能。活动子主题(或活动主题)的php文件。 已测试并正常工作。


    设置dispayed 大车总计 对于2个小数,请使用此值 (仅适用于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;
    }
    

    代码进入功能。活动子主题(或活动主题)的php文件。 已测试并正常工作。

    你不能在购物车总价表中对价格进行四舍五入。如果使用不同的钩子执行此操作,则会出现计算错误。我的代码只是更改显示的格式化价格的小数位数,不会改变实际价格的计算

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

        2
  •  0
  •   dipmala    7 年前

    若您只想在购物车和结帐页面更改购物车总数,那个么您需要在主题中复制模板

    首先将woocommerce插件模板中的2个文件复制到主题中。

    1) 复制 插件\ woocommerce \模板\购物车\购物车总计。php 您的主题文件夹\woocommerce\cart\cart总计。php

    2) 复制 插件\woocommerce\templates\checkout\review order。php 您的主题文件夹\woocommerce\checkout\review order。php

    在这两个文件中找到 wc\u cart\u totals\u order\u total\u html() 注释此代码并将其放在下面的代码中。

      $args=array('decimals'=> 2);
      echo  wc_price(WC()->cart->total,$args); 
    

    此代码经过测试,工作正常。希望它也能帮助你。

        3
  •  0
  •   frezq    6 年前

    在我的情况下,我需要在整个网站中保持3个小数位“可见”,但仅将总数近似为2个小数位(第三个小数位为零),因为我的支付网关只接受小数位后的2个有效数字。

    Prices displayed as: 0.336
    Taxes: 0.017
    But grand total needed to be: 0.350 (instead of 0.353)
    

    我最终没有使用该代码,因为它太可怕了,但你可以说这是一种心理锻炼:

    add_filter( 'wc_get_price_decimals', 'change_prices_decimals', 20, 1 );
    function change_prices_decimals( $decimals ){
    
        if( is_cart() || is_checkout() )
        {
            $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,0);
            $length = count($trace);
            for ($i = 0; $i < $length; $i++)
            {
                if($trace[$i]["function"] == "set_total"){
                    $decimals = 2;
                    return $decimals;
                }
            }
        }
    
        return $decimals;
    }