Additional CCK powered price for product in Ubercart
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 field_p_unique_price)
2. In your custom module, implement uc_price_handler hook to tell Ubercart that we have additional price processing function now:
<?php
function YOURMODULE_uc_price_handler() {
return array(
'alter' => 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'];
}
}
}
?>3. 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.
Anton Sidashin
senior developer, Pixeljets co-founder
I'm a web developer specializing in PHP and Javascript, and Drupal, of course. I'm building Drupal projects since 2005, and I was working as full-time senior engineer in CS-Cart for a while, building revolutionary e-commerce software. In my free time, I enjoy playing soccer, building my body in gym, and playing guitar.
Drupal.org ID: restyler
Комментарии
Similar results can be achieved by http://drupal.org/project/uc_price_per_role - although not quite as flexible as this custom code solution it does allow custom prices per role.
Absolutely. Like I’ve said, there is a bunch of modules to modify price.
Nice quick little solution for that, thanks for sharing. I'm starting to get more and more involved with Ubercart so it's always good to read little pointer blog posts such as this one!
You’re welcome :)
Hi! I'm new to Drupal. Can i use "ubercart" in my "google site"?
Could this be adjusted to calculate the sell price based on the total sold of a product or current stock level?
Yes, since you have $context variable which holds all the necessary data.
I am looking for some help writing a price handler that would change the price based on overall volume of the product sold. Similar to volume pricing, but the pricing would be based the total number sold of a product, rather than the total number sold to one user.
So if I had an item that has a sell price on $1.00 with price breaks if the total number sold is greater than or equal to x (entered via a cck field) the price would be x (entered via a cck field)
If I sold 1000 total piece to all users the price would drop to the associated volume price amount.
Let me know if you are interested. You can email me at krista@peacelovejava.com. I can compensate you for the work.
This is really good to see articles in this topic...
Students must work as part time.. It will improve their kwledge as well as their confident...
physical fitness
Комментировать