How to apply bulk discount in WooCommerce?

In this tutorial we’ll learn how to apply bulk discount in Woocommerce. There are number of ways to offer customers discount. In this tutorial we’ll do this with simplest possible way.

We’ll make the process dynamic where bulk discount can be applied depending on cart total. We’ll offer 5% discount on orders 500 and above and 10% on orders 1000 and above.

So, to do this adds the following lines of code at the end of your theme’s functions.php file:

add_action('woocommerce_cart_calculate_fees', 'tl_apply_bulk_discount');
function tl_apply_bulk_discount() {
  global $woocommerce;
  $excluded_amount = $discount_percent = 0;
  $working_total   = $woocommerce->cart->cart_contents_total;
  $excluded_categories = array(
    217, # Training
    223, # Starter Kits
  );
 
  # Only apply manual discount if no coupons are applied
  if (!$woocommerce->cart->applied_coupons) {
 
    # Find any items in cart that belong to the restricted categories
    foreach ($woocommerce->cart->cart_contents as $item) {
      $product_categories = get_the_terms($item['product_id'], 'product_cat');
      if (empty($product_categories) || is_wp_error($product_categories) || !$product_categories) {
        if (is_wp_error($product_categories)) {
          wp_die($product_categories->get_error_message());
        }
        else {
          $product_categories = new WP_Error('no_product_categories', "The product \"".$item->post_title."\" doesn't have any categories attached, thus no discounts can be calculated.", "Fatal Error");
          wp_die($product_categories);
        }
      }
      foreach ($excluded_categories as $excluded_category) {
        foreach ($product_categories as $category) {
          if ($excluded_category == $category->term_id) {
            $excluded_amount += $item['line_subtotal']; # Increase our discounted amount
            $working_total -= $item['line_subtotal'];   # Decrease our discounted amount
          }
        }
      }
    }
 
    # Logic to determine WHICH discount to apply based on subtotal
    if ($working_total >= 500 && $working_total < 1000) {
      $discount_percent = 5;
    }
    elseif ($working_total >= 1000) {
      $discount_percent = 10;
    }
    else {
      $discount_percent = 0;
    }
 
    # Make sure cart total is eligible for discount
    if ($discount_percent > 0) {
      $discount_amount  = ( ( ($discount_percent/100) * $working_total ) * -1 );
      $woocommerce->cart->add_fee('Bulk Discount', $discount_amount);
    }
  }
}

Add the code to your theme’s functions file and Click update to save the file.

Now, when the cart total will be 500 and above, 5% discount will automatically be applied:

2015-10-20_1300

You can change Amount and Discount percentage from the above code according to your needs. Thanks

Editorial Staff

ThemeLocation Provides Wordpress & Woocommerce development & Customization services. Have a Project? Contact To discuss

Leave a Reply

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