Additional CCK powered price for product in Ubercart

Table of Contents

I was not updating my blog for a while - it's summer here :)
I've just returned back from my business trip to US, Alaska, where I helped guys from shipito.com killer to automate their warehouse and create good user control panel in their website (everything is Drupal-powered here!). I've also spent several days surfing in San Diego. It's so exciting to realize that Drupal knowledge can give you an opportunity to visit such amazing places and meet interesting and smart people all over the world!

Today I will tell you about small, but cool feature which makes Ubercart website pricing even more flexible.
The code was designed to work in Drupal 6.x + Ubercart 2.4.

There is a lot of Ubercart modules that provide separate prices for some specific conditions.
But here is a very simple method to implement your own separate price for products - you can customize it to be very unique - so, for example, product can have different price in the afternoon or in specific day, or when user has exactly two specific roles at the same time. And the price can be retrieved from additional CCK field or any other source.

In my case, it was necessary to show separate 'retail prices' to visitors, when 'retail prices' checkbox was activated by them in the website interface. For admin, it's just a cck field on node edit page: 'Retail price:'

But in this example, I will simplify things a bit - the price will be modified for user with uid=32.

  1. So, add new CCK float field to the product node type (let's say, the name for the field is fieldpunique_price)
  2. In your custom module, implement ucpricehandler hook to tell Ubercart that we have additional price processing function now:

array(       'title' => t('My super price handler'),       'description' => t('Handles unique prices for user 32.'),       'callback' => 'YOURMODULE_price_alterer',     ),   ); }  function YOURMODULE_price_alterer(&$price_info, $context, $options = array()){     global $user;   // if user id is 32...   // modify the price   if ($user->uid == 32) { // modify this line to your needs!     if ($context["subject"]["node"]->field_p_unique_price[0]['value']) {       $price_info["price"] = $context["subject"]["node"]->field_p_unique_price[0]['value'];     }   } }  ?>

  1. Now go to Ubercart price processing page: admin/store/settings/price-handlers
    If you've done everything right, you should see that your price modifier function appeared in the list of price processing functions, and it is enabled.
    There is a price caching checkbox here, it's important to disable it for now.

Now log in as user #32 and check the product node with has unique price populated.
You should see the price that differs from price for all other users.