【问题标题】:Remove Cents (Round Up to Nearest Dollar) on WooCommerce Orders删除 WooCommerce 订单上的美分(四舍五入到最接近的美元)
【发布时间】:2018-11-28 18:31:53
【问题描述】:

目前使用以下代码对显示价格进行四舍五入,但是创建的订单和所有订单电子邮件不会对总数进行四舍五入。有什么方法可以在下订单后不恢复的每个订单上保留整数?

add_filter( 'woocommerce_calculated_total', 'round_price_product' 
);
function round_price_product( $price ){
    // Return rounded price
    return round( $price );
}

谢谢

【问题讨论】:

    标签: php wordpress woocommerce rounding orders


    【解决方案1】:

    可以使用 woocommerce_get_formatted_order_total 过滤钩舍入订单总价在格式化为显示在订单总行之前完成:

    add_filter( 'woocommerce_get_formatted_order_total', 'round_formatted_order_total', 10, 2 );
    function round_formatted_order_total( $formatted_total, $order ) {
    
        $formatted_total = wc_price( round( $order->get_total() ), array( 'currency' => $order->get_currency() ) );
    
        return $formatted_total;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试和工作。


    或者,如果您需要对所有订单总数进行四舍五入,您将使用 woocommerce_get_order_item_totals 挂钩,您必须在其中四舍五入您需要的所有行。

    在下面的示例中,小计和总计行将被四舍五入:

    add_filter( 'woocommerce_get_order_item_totals', 'rounded_formatted_order_totals', 10, 3 );
    function rounded_formatted_order_totals( $total_rows, $order, $tax_display ) {
        $tax_display = $tax_display ? $tax_display : get_option( 'woocommerce_tax_display_cart' );
    
        // For subtotal line
        if ( isset( $total_rows['cart_subtotal'] ) ) {
            $subtotal    = 0;
            foreach ( $order->get_items() as $item ) {
                $subtotal += $item->get_subtotal();
                if ( 'incl' === $tax_display ) {
                    $subtotal += $item->get_subtotal_tax();
                }
            }
            $subtotal = wc_price( round( $subtotal ), array( 'currency' => $order->get_currency() ) );
            if ( 'excl' === $tax_display && $this->get_prices_include_tax() ) {
                $subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
            }
            $total_rows['cart_subtotal']['value'] = $subtotal;
        }
    
        // For total line
        if ( isset( $total_rows['order_total'] ) ) {
            $total = wc_price( round( $order->get_total() ), array( 'currency' => $order->get_currency() ) );
            $total_rows['order_total']['value'] = $total;
        }
    
        return $total_rows;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试和工作。

    【讨论】:

      猜你喜欢
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多