Professional Drupal Web Developers in Atlanta, GA

How to Create and Add Quantity Discounts Per Item with Ubercart 2

How to Create and Add Quantity Discounts Per Item with Ubercart 2

There are times when, as a Drupal developer, you are presented a problem which you are sure there must be a contributed module already available to help you. This recently happened to us, and unfortunately there was not a module that would successfully do what we needed.

On a recent Drupal/Ubercart (D6/UC2) ecommerce project we were presented with a situation whereby individual products would receive quantity discounts. These were strictly on a per-item basis, that is to say that when a user added a specific item to their cart, the per-item price for that item would be dependent upon the quantity of that specific item in their cart. Some products had discount levels at quantities of 25-99 and 100+ while some products only offered a discount for quantities of 100+.

We tried the Ubercart Price List module, the Ubercart Discounts (Alternative) module, the Ubercart Bulk Discount module, and of course the built-in Ubercart Discounts module. While many of these had great features and some came close to what we needed, nothing quite hit the bullseye. After much more searching, we finally found the best solution to our problem. The solution was to use a simple implementation of the function, hook_cart_item, from the Ubercart API.

Below is a generalized version of the first scenario mentioned above (5% off for 25-99, 10% off for 100+ for ALL products) where the name of your custom module would be in place of "module" in the function name, module_cart_item:

/*
 * Implementation of hook_cart_item
 */
function module_cart_item($op, &$item) {
  switch ($op) {
    case 'load':
      if ($item->qty >= 25 && $item->qty < 100) {
        $item->price *= .95;
      }
      elseif ($item->qty >= 100) {
        $item->price *= .9;
      }
      break;
  }
}

This function can be defined in more detail such that different product classes or individual products receive their own quantity discounts.