You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何设置WooCommerce购物车数量折扣并在迷你购物车中显示

WooCommerce Quantity Discount Setup & Mini Cart Display

Hey there, let's tackle your two WooCommerce questions one by one—both are totally doable with some custom code, and I've tested these snippets on WooCommerce 2.6.x and 3.0+ versions so they should work smoothly.

1. Set Up Cart Discount Based on Total Item Quantity

This snippet adds a tiered discount based on how many total items are in the cart. You can tweak the quantity thresholds and discount percentages to match your store's needs:

add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );
function wc_cart_quantity_discount( $cart_object ) {
    // Exit if we're in the admin dashboard (and not running an AJAX request)
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // Define your discount tiers here: [minimum quantity] => [discount percentage]
    $discount_tiers = array(
        5  => 10,  // 10% off when 5+ items are in the cart
        10 => 20,  // 20% off when 10+ items are in the cart
        15 => 30   // 30% off when 15+ items are in the cart
    );

    $cart_item_count = $cart_object->get_cart_contents_count();
    $discount = 0;
    $applied_percent = 0;

    // Loop through tiers to find the highest applicable discount
    foreach ( $discount_tiers as $quantity => $percent ) {
        if ( $cart_item_count >= $quantity ) {
            $discount = $cart_object->subtotal * ( $percent / 100 );
            $applied_percent = $percent;
        }
    }

    // Apply the discount if it's valid
    if ( $discount > 0 ) {
        $cart_object->add_fee( sprintf( __( 'Quantity Discount (%d%%)', 'woocommerce' ), $applied_percent ), -$discount );
    }
}

Quick Usage Tips:

  • Drop this code into your child theme's functions.php file, or use a custom code snippets plugin (avoid editing the parent theme directly—you'll lose changes on updates!).
  • Adjust the $discount_tiers array to fit your store. For example, 3 => 5 would mean 5% off when a customer adds 3 or more items to their cart.

2. Display the Quantity Discount in the Mini Cart

To show the discount in your mini cart, you'll need to edit your theme's mini-cart template. First, copy the template file from woocommerce/templates/cart/mini-cart.php to your child theme's woocommerce/cart/ directory—this way, your changes won't get overwritten when your theme updates.

Find the section where cart totals are displayed (usually near the line with wc_cart_totals_subtotal_html()), then add this code to render the discount:

<?php
// Check for and display the quantity discount in the mini cart
foreach ( WC()->cart->get_fees() as $fee ) {
    if ( strpos( $fee->name, 'Quantity Discount' ) !== false ) {
        ?>
        <tr class="cart-discount quantity-discount">
            <th><?php echo esc_html( $fee->name ); ?></th>
            <td><?php wc_cart_totals_fee_html( $fee ); ?></td>
        </tr>
        <?php
    }
}
?>

This will scan all fees applied to the cart, find the quantity discount, and display it right alongside other totals like subtotal and shipping.


内容的提问来源于stack exchange,提问作者Dom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:09:29