显示 WooCommerce 购物车和生成的订单中的总节省金额

问题描述 投票:0回答:1

我使用以下代码在结账页面显示总储蓄金额:

add_action( 'woocommerce_review_order_after_order_total', 'show_total_discount_cart_checkout', 1000 );
function show_total_discount_cart_checkout() {
    $discount_total = 0; // Initializing

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $item ) {
        $product = $item['data'];

        if ( $product->is_on_sale() ) {
            $regular_args = array( 'price' => $product->get_regular_price() );

            if ( WC()->cart->display_prices_including_tax() ) {
                $active_price    = wc_get_price_including_tax( $product );
                $regular_price   = wc_get_price_including_tax( $product, $regular_args );
            } else {
                $active_price    = wc_get_price_excluding_tax( $product );
                $regular_price   = wc_get_price_excluding_tax( $product, $regular_args );
            }
            $discount_total += ( $regular_price - $active_price ) * $item['quantity'];
        }
    }

    if ( WC()->cart->display_prices_including_tax() ) {
        $discount_total += WC()->cart->get_discount_tax();
    }

    $discount_total += WC()->cart->get_discount_total();

    if ( $discount_total > 0 ) {
        $text = __("SÄÄSTÖSI", "woocommerce");

        printf( '<tr class="total-saved"><th>%s</th><td data-title="%s">-%s</td></tr>', $text, $text, wc_price($discount_total) );
    }
}

我还想在购物车页面和生成的订单中显示总储蓄金额。好像是负责客户订单的order-details.php模板。

如何在购物车页面和生成的订单中显示总节省金额?

php woocommerce cart orders discount
1个回答
0
投票

几乎没有什么东西可以显示购物车和生成订单中的总节省金额。

在下面,我们将将该总节省金额保存为自定义订单元数据,以在客户订单和电子邮件通知中显示总节省金额。

尝试以下修改后的代码替换:

// Utility function: Get cart total savings amount
function get_cart_total_savings() {
    $total_saved = 0; // Initializing

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $item ) {
        $product = $item['data'];

        if ( $product->is_on_sale() ) {
            $regular_args = array( 'price' => $product->get_regular_price() );

            if ( WC()->cart->display_prices_including_tax() ) {
                $active_price    = wc_get_price_including_tax( $product );
                $regular_price   = wc_get_price_including_tax( $product, $regular_args );
            } else {
                $active_price    = wc_get_price_excluding_tax( $product );
                $regular_price   = wc_get_price_excluding_tax( $product, $regular_args );
            }
            $total_saved += ( $regular_price - $active_price ) * $item['quantity'];
        }
    }

    if ( WC()->cart->display_prices_including_tax() ) {
        $total_saved += WC()->cart->get_discount_tax();
    }

    return $total_saved + WC()->cart->get_discount_total();
}

// Display total savings amount in cart and checkout pages
add_action( 'woocommerce_cart_totals_after_order_total', 'display_cart_total_savings_amount', 1000 ); // Cart
add_action( 'woocommerce_review_order_after_order_total', 'display_cart_total_savings_amount', 1000 ); // Checkout
function display_cart_total_savings_amount() {
    $total_saved = get_cart_total_savings();

    if ( $total_saved > 0 ) {
        $label = __('Savings', 'woocommerce');

        printf( '<tr class="total-saved"><th>%s</th><td data-title="%s">-%s</td></tr>', $label, $label, wc_price($total_saved) );
    }
}

// Save the total savings amount as custom order metadata
add_action( 'woocommerce_checkout_create_order', 'add_cart_total_savings_amount_metadata' );
function add_cart_total_savings_amount_metadata( $order ) {
    $total_saved = get_cart_total_savings();

    if ( $total_saved > 0 ) {
        $order->add_meta_data( 'total_saved', $total_saved, true );
    } 
}

// Display total savings amount on customer orders and email notifications
add_filter( 'woocommerce_get_order_item_totals', 'display_total_savings_on_order_item_totals', 10, 3 );
function display_total_savings_on_order_item_totals( $total_rows, $order, $tax_display ) {
    if ( $total_saved = $order->get_meta('total_saved') ) {
        $total_rows['total_saved'] = array(
            'label' =>__('Savings', 'woocommerce') . ':',
            'value' => wc_price($total_saved, array( 'currency' => $order->get_currency() )),
        );
    }
    return $total_rows;
}

代码位于子主题的functions.php文件中(或插件中)。

在购物车页面:

enter image description here

按收到页面的顺序:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.