More sales with the step discount technique based on the number of purchases

More sales with the stepped discount technique based on the number of purchases in WooCommerce online stores

Give your customers a discount based on the number of items they add to their cart. For example, if they have one item in their cart, they'll see a message saying that if they add a second item, they'll get a 5% discount. If they add three items, they'll get an 8% discount. And if they purchase more than five items, they'll receive a 15% discount.    


// Automatically apply Woocommerce coupon based on cart quantity
add_action( 'woocommerce_before_calculate_totals', 'rezvan_autoapply_coupon', 25, 1 );
function rezvan_autoapply_coupon( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Setting and initialising variables
    $coupon = 'test20'; // Here goes your coupon code
    $item_count = 4; // Here goes the number of items
    $matched    = false;

    if( $cart->cart_contents_count >= $item_count ){
        $matched = true; 
    }

    // If conditions are matched add coupon is not applied
    if( $matched && ! $cart->has_discount( $coupon )){
        // Apply the coupon code
        $cart->add_discount( $coupon );

        // Optionally display a message
        wc_add_notice( __('20% discount coupon added'), 'notice');
    }
    // If conditions are not matched and coupon has been appied
    elseif( ! $matched && $cart->has_discount( $coupon )){
        // Remove the coupon code
        $cart->remove_coupon( $coupon );

        // Optionally display a message
        wc_add_notice( __('Sorry, discount is not available'), 'error');
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × 2 =

 
Online-Chat

We are ready to answer your questions