Categories
Drupal Development Resources

Node checkout with quantities

The Node Checkout module is a great way to allow users to customize a product with a Drupal + Ubercart setup. However, one of the flaws is that it doesn’t allow you to change the quantity of the item in the cart after it’s been added.

For example, you may use node checkout to customize a greeting card design for the holidays. After customizing the product, you really like the design and now you want to order a few more. With the default method, you won’t be able to edit the quantity. The visitor would have to delete the item from the cart, then start all over. Obviously, that’s not ideal and the user may get frustrated and leave the shopping cart.

Problem: Users can’t change the quantity after adding a node checkout customized item to the cart.
Solution: Use CCK and a custom Drupal module to move the quantity field to the node checkout page.

Tested on: Drupal 6.22 + Ubercart 2.4

The default behavior in Ubercart is put the quantity and attribute selections on the “product” page. Normally you could add a product, then go back and edit the quantity after adding it to your cart. After creating a node with node checkout, Ubercart does not let you return to the product page and the text field for the quantity is disabled on the cart page. However, it does generate a link so you can edit the node_checkout node as many time as you’d like.

The workaround, and perhaps an even better workflow, is to “move” the quantity field to the node checkout page, so that all options are on a single page. To do this, simply create a CCK field for “quantity” in the content type you’re using for node checkout.

The trick now is how to tell Ubercart to set the quantity of the item to this CCK field. To do that, we use a simple custom module using hook_cart_item. Replace [module_name] with the name of your own module.

hook_cart_item can be used to do a number of other useful things, like setting the price of a product based on quantity entered. Use cases would be tiered pricing based on quantity or another cck value, like a custom sign maker that charges by the number of number of letters. In this example, the price is adjusted based on the quantity, with discounts for larger quantities:

function [module_name]_cart_item($op, &$item) {
  switch ($op) {
    case 'load':
      // load the node data from the node_checkout node
      $item_info = node_load($item->data['node_checkout_nid']);
      // set the cart quantity equal to the field value
      $item->qty = $item_info->field_quantity[0]['value'];
      // quantities of 10 or more have a 10% discount
      if ($item->qty >= '10') {
        $item->price = $item->price * .9;
      }
   }
}

Leave a Reply

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

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