I like lemonade.

Ubercart 2.x Price Handler and Old Price

Share |

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 contribution 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.

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><p>';
        $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

Hi Yannick, in this example

Hi Yannick,

in this example you just check if the price been changed from the "original price" and if it did you print the "original price" as the "old price". I am afraid, you have to do the price changing somewhere else.

You would normally let the Discount module or something similar to change your price.
Also, please note, that this article is a bit outdated now, so the information posted here might not be relevant any-more.

Hi Great module. Where do you

Hi

Great module.

Where do you specify the new price in node editing form ?

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> <img> <p> <br>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options