The Sheriff is an idiot. I've met smarter sandwiches

Ubercart 2.x Price Handler and Old Price

Ubercart 2.x introduced a new way of handling product prices. It is called price_handler().

This is very nice, and most of the discount and TAX modules are using this new function to override the product price, but what if you need the new price and the old price on the same product?

Let's say you would like to show your visitors the product price before it was discounted.

You obviously should not change the condribution modules or the Ubercart core. You should just simply create your own module or use the general module you have created earlier (You got one, right?) and just paste the following lines at the bottom.

This function will create the price handler. Don't forget to replace 'your_module' with your module name everywhere.

/**
* Implementation of hook_uc_price_handler().
*/
function your_module_uc_price_handler() {
return array(
'alter' => array(
'title' => t('Old price handler'),
'description' => t('Adds old price to product.'),
'callback' => 'your_module_price_handler_alter',
),
);
}

 

This function will save the original Sell Price and put it on the node object for further use.

/**
* This is just a function to save the original sell price and put it on the node.
*/
function your_module_price_handler_alter(&$price_info, &$context, &$options) {
if (isset($context['subject']['node'])
&& $context['subject']['field'] == 'sell_price'
&& !isset($context['subject']['node']->old_price)) {
$node = $context['subject']['node'];
$context['subject']['node']->old_price = $node->sell_price;
}
}

One you have enabled your module, you will have the old_price on the node object so you can play with it. :)

/**
* hook_nodeapi()
*/
function your_module_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
switch ($op) {
case 'view':
if( ($node->sell_price != $node->old_price) && ($node->old_price > 0) ){
$old_price_content = '<div class="old-price-wrapper">';
$old_price_content .= '<div class="old-price">'.t('Was: ')';
$old_price_content .= '<span class="price-was">';
$old_price_content .= uc_currency_format($node->old_price).'</span>';
$old_price_content .= '</div></div>';
$node->content['old_price']['#value'] = $old_price_content;
$node->content['old_price']['#weight'] = -1;
}
break;
}
}

 

If you have any questions, just ask it in a comment. Good luck.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <code>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.