Search the site:

Home Blog How to offer discounts based on quantity in WooCommerce

How to offer discounts based on quantity in WooCommerce

Commonly, as a store owner, you may sometimes want to offer discounts if customers buy more than one quantity of a particular product. This may either be to boost sales or finish excess stocks. In this post, you will learn how to offer discounts based on quantity in WooCommerce using both code snippets and plugins.

How to offer discounts based on quantity in WooCommerce using Code Snippets

Let’s explore how to use a code snippet to do this. With this code snippet, we are offering customers a discount of 5% if they purchase over 2 quantities of the same product.

To do this, you will need to add the code below to the functions.php file of your child theme:

add_filter('woocommerce_add_cart_item_data', 'ts_add_default_price_as_custom', 20, 3 );

function ts_add_default_price_as_custom( $cart_item_data, $product_id, $variation_id ){

    $product_id = $variation_id > 0 ? $variation_id : $product_id;

    ## ----- SET THE DISCOUNT HERE ----- ##
    $discount_percentage = 5; // Discount (5%)

    // The WC_Product Object
    $product = wc_get_product($product_id);

    $price = (float) $product->get_price();

    // Set the Product default base price as custom cart item data
    $cart_item_data['base_price'] = $price;

    // Set the Product discounted price as custom cart item data
    $cart_item_data['new_price'] = $price * (100 - $discount_percentage) / 100;

    // Set the percentage as custom cart item data
    $cart_item_data['percentage'] = $discount_percentage;

    return $cart_item_data;
}

// Display the product original price
add_filter('woocommerce_cart_item_price', 'ts_display_cart_items_default_price', 20, 3 );

function ts_display_cart_items_default_price( $product_price, $cart_item, $cart_item_key ){
    if( isset($cart_item['base_price']) ) {
        $product        = $cart_item['data'];
        $product_price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $cart_item['base_price'] ) ) );
    }
    return $product_price;
}

// Display the product name with the discount percentage
add_filter( 'woocommerce_cart_item_name', 'ts_add_percentage_to_item_name', 20, 3 );
function ts_add_percentage_to_item_name( $product_name, $cart_item, $cart_item_key ){
    if( isset($cart_item['percentage']) && isset($cart_item['base_price']) ) {
        if( $cart_item['data']->get_price() != $cart_item['base_price'] )
            $product_name .= ' <em>(' . $cart_item['percentage'] . '% discounted)</em>';
    }
    return $product_name;
}

add_action( 'woocommerce_before_calculate_totals', 'ts_custom_discounted_cart_item_price', 20, 1 );
function ts_custom_discounted_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    ## ----- SET THE QUANTITY HERE ----- ##
    $targeted_qty = 2; // Targeted quantity

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {

        // For item quantity of 2 or more
        if( $cart_item['quantity'] >= $targeted_qty && isset($cart_item['new_price']) ){

            // Set cart item discounted price
            $cart_item['data']->set_price($cart_item['new_price']);
        }
    }
}

The above code will add a discount of 5% if the quantity of the product is above 2. You can change this to suit your requirements by changing the $discount_percentage variable to set the desired discount, and the $targeted_qty to set the quantity above which you wish to apply the discount.

offer discounts based on quantity in WooCommerce - Updated Cart contents

You will now be able to see a discounted price if you add 2 or more quantities of the same product in your cart.

We have used four major hooks in the above code:

woocommerce_add_cart_item_data : This filter hook is used to change the contents / meta data of the cart.
woocommerce_cart_item_price : This filter hook is used to change the way prices are displayed in the cart.
woocommerce_cart_item_name : This filter hook can be used to change the product name in the cart. In our example, we are using it to append the discount percentage to the product name.
woocommerce_before_calculate_totals : This action hook is used to override the product price in the cart.

How to offer discounts based on quantity only for items that are NOT on Sale:

If you want to restrict this to only those products which are not on sale, you simple have to edit the code snippet above to add a condition inside the function that is added to the hook woocommerce_add_cart_item_data:

add_filter('woocommerce_add_cart_item_data', 'ts_add_default_price_as_custom', 20, 3 );
function ts_add_default_price_as_custom( $cart_item_data, $product_id, $variation_id ){
    $product_id = $variation_id > 0 ? $variation_id : $product_id;

    ## ----- SET THE DISCOUNT HERE ----- ##
    $discount_percentage = 5; // Discount (5%)

    // The WC_Product Object
    $product = wc_get_product($product_id);

    // ADD CONDITION HERE to offer DISCOUNT ONLY for NON On-Sale Products
    if( ! $product->is_on_sale() ){
        $price = (float) $product->get_price();

        // Set the Product default base price as custom cart item data
        $cart_item_data['base_price'] = $price;

        // Set the Product discounted price as custom cart item data
        $cart_item_data['new_price'] = $price * (100 - $discount_percentage) / 100;

        // Set the percentage as custom cart item data
        $cart_item_data['percentage'] = $discount_percentage;
    }

    return $cart_item_data;
}

// Display the product original price
add_filter('woocommerce_cart_item_price', 'ts_display_cart_items_default_price', 20, 3 );
function ts_display_cart_items_default_price( $product_price, $cart_item, $cart_item_key ){
    if( isset($cart_item['base_price']) ) {
        $product        = $cart_item['data'];
        $product_price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $cart_item['base_price'] ) ) );
    }
    return $product_price;
}

// Display the product name with the discount percentage
add_filter( 'woocommerce_cart_item_name', 'ts_add_percentage_to_item_name', 20, 3 );
function ts_add_percentage_to_item_name( $product_name, $cart_item, $cart_item_key ){
    if( isset($cart_item['percentage']) && isset($cart_item['base_price']) ) {
        if( $cart_item['data']->get_price() != $cart_item['base_price'] )
            $product_name .= ' <em>(' . $cart_item['percentage'] . '% discounted)</em>';
    }
    return $product_name;
}

add_action( 'woocommerce_before_calculate_totals', 'ts_custom_discounted_cart_item_price', 20, 1 );
function ts_custom_discounted_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    ## ----- SET THE QUANTITY HERE ----- ##
    $targeted_qty = 2; // Targeted quantity

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {

        // For item quantity of 2 or more
        if( $cart_item['quantity'] >= $targeted_qty && isset($cart_item['new_price']) ){

            // Set cart item discounted price
            $cart_item['data']->set_price($cart_item['new_price']);
        }
    }
} 

In this way, you can offer discounts based on quantity using code snippets for non-sale, sale or all products i.e. store-wide by stating the appropriate condition inside the function that is added to the filter hook woocommerce_add_cart_item_data.

How to offer discounts based on quantity in WooCommerce using Plugins

If you are not comfortable with adding a code snippet to your website files, you can use plugins to do this too:

  1. Discount Rules for WooCommerce: This is a free plugin that lets you add percentage discounts not just based on quantities of products, but also if your customers exceed a certain price limit. You can also add percentage discounts on a certain number of items (not necessarily of the same product). The PRO version of this plugin allows you to add category-wide discounts, BOGO (Buy One Get One Free) deals, a fixed price discount instead of a percentage discount, and more.
  2. Conditional Discounts for WooCommerce: This is another plugin that offers nearly all important features in its free version- You can add discounts based on quantity, number of cart items, for specific categories, on specific products, on variable features (blue shirt, size Medium etc.), for certain shop roles or even specific customers. Its PRO version has a few additional features one of them being adding a product free of cost to the cart on purchase of a certain product.

In this way, you may use either code snippets or plugins to offer discounts to your customers based on quantities of products.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of
from purchased

Subscribe for more offers