Apply Cart Discount Based on Quantity

This snippet applies a discount to the cart based on the total quantity of items, encouraging bulk purchases.

PHP

function apply_cart_quantity_discount( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    $total_quantity = $cart->get_cart_contents_count();
    $discount_percentage = 0;

    if ( $total_quantity >= 10 && $total_quantity < 20 ) {
        $discount_percentage = 5;
    } elseif ( $total_quantity >= 20 ) {
        $discount_percentage = 10;
    }

    if ( $discount_percentage > 0 ) {
        $discount = ( $cart->subtotal / 100 ) * $discount_percentage;
        $cart->add_fee( 'Bulk Discount', -$discount );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'apply_cart_quantity_discount' );

Snippet Feedback

Did this snippet work for you? Do you have any questions about this snippet? Leave some feedback below.

Tags

Language

Did it work?

Save Snippet

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.