【问题标题】:Add calculated saving total in Woocommerce order totals rows在 Woocommerce 订单总计行中添加计算的储蓄总计
【发布时间】:2019-02-28 13:09:24
【问题描述】:

在 Woocommerce 中,我使用以下代码计算并在购物车和结帐页面中的订单上显示“总节省”:

function wc_discount_total_30() {
    global $woocommerce;
    $discount_total = 0;
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {
        $_product = $values['data'];
        if ( $_product->is_on_sale() ) {
            $regular_price = $_product->get_regular_price();
            $sale_price = $_product->get_sale_price();
            $discount = ($regular_price - $sale_price) * $values['quantity'];
            $discount_total += $discount;
        }
    }
    if ( $discount_total > 0 ) {
        echo '<tr class="cart-discount">
        <th>'. __( 'Saved', 'tsavedis' ) .'</th>
        <td data-title=" '. __( 'Saved', 'tsavedis' ) .' ">'
        . wc_price( $discount_total + $woocommerce->cart->discount_cart ) .'</td>
        </tr>';
    }
}

// Hook our values to the Basket and Checkout pages
add_action( 'woocommerce_cart_totals_after_order_total', 'wc_discount_total_30', 99);
add_action( 'woocommerce_review_order_after_order_total', 'wc_discount_total_30', 99);

我需要在后端的“订单编辑”页面中显示此总节省作为自定义字段。
该怎么做?

【问题讨论】:

    标签: php wordpress woocommerce orders discount


    【解决方案1】:

    以下是将相同的内容添加到订单总计表的方法:

    // Display the chosen delivery information
    add_filter( 'woocommerce_get_order_item_totals', 'add_saving_total_order_totals', 10, 3 );
    function add_saving_total_order_totals( $total_rows, $order, $tax_display ) {;
        $saving_total = 0;
    
        // Loop through Order items
        foreach($order->get_items() as $item ){
            $product = $item->get_product();
    
            if( $product->is_on_sale() ){
                $regular_price   = (float) $product->get_regular_price();
                $active_price    = (float) $product->get_price();
    
                $saving_total   += ($regular_price - $active_price) * $item->get_quantity();
            }
        }
    
        if( $saving_total > 0 ) {
            $discount_total = $order->get_discount_total();
    
            $label  = __( 'Saved', 'tsavedis' );
            $value  = wc_price( $saving_total + $discount_total );
    
            $total_rows['saving'] = array( 'label' => $label,'value' => $value );
        }
        return $total_rows;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且有效。

    【讨论】:

    • 谢谢 它就像一个魅力,但它不会在金额前显示卢比符号 (₹),而是显示一个小方块。我该如何解决这个问题?
    • 是的,现在它显示了卢比符号。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多