5 replies [Last post]
pferlito's picture
Offline
Joined: 10/06/2009
Juice: 15
Was this information Helpful?

My goal is to discount items according to role using a hook.
I've created a hook in my module, bg_helper:

function bg_helper_uc_price_handler() {
  print "at hook";

  return array(
    'alter' => array(
      'title' => t('My price handler'),
      'description' => t('Handles my price alteration needs.'),
      'callback' => 'my_price_handler_alter',
    )
  );
}
function my_price_handler_alter () {
  print "at price handler";
}

The my_price_handler_alter() function gets called, but I cannot find
the documentation that tells me what the arguments are for the callback.

Can someone please tell me what arguments are passed to the price
alteration callback?

Thanks for your help.

Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6846
Re: price handler callback arguments

In the last section of http://www.ubercart.org/docs/developer/11375/price_api, under "Hook functions and theming", it says the alteration functions take 3 arguments: price_info, context, and options.

pferlito's picture
Offline
Joined: 10/06/2009
Juice: 15
price handler callback arguments

Thanks Lyle for your reply. From reading the documentation my understanding
is that I need to change the price info "price" value and set the context
"revision" value to "altered". However, this is not changing the price
displayed in the cart. Here is the code I have in the callback:

function my_price_handler_alter ($price_info,$context,$options) {
  $price_info['price'] = "13.95";
  $context['revision'] = 'altered';
}
pferlito's picture
Offline
Joined: 10/06/2009
Juice: 15
price handler callback arguments

I figured it out. The arguments are passed by reference to the
price handler, which makes perfect sense when I think about it.

gbernier's picture
Offline
Joined: 09/13/2010
Juice: 3
Hey pferlito, Did you get the

Hey pferlito,

Did you get the price handlers to work for you? I've implemented one on a project I'm doing and have learnt the hard way why the other developers lost hair having to deal with Ubercart pricing.

Issues I'm experiencing on an unaltered Ubercart Core:
- Total isn't being calculated using the price handlers on first page of checkout
- On review order page the line item with altered price doesn't get formated
- On review order page the subtotal and total are only showing the original price

Based on my experience so far running down the rabbits hole it's been brutal and wonder if the price handler functions were completed. Did you have success with your setup?

Including my code incase I'm doing something wrong

/**
* Implementation of hook_uc_price_handler().
*/
function test_additional_license_uc_price_handler() {
  return array(
    'alter' => array(
      'title' => t('Set the additional licenese fee discount.'),
      'description' => t('Set the additional licenese fee discount when more then one product, which has an additional license fee set.'),
      'callback' => 'test_additional_license_alter',
    ),
  );
}
/**
* @param $price_info
* Contains the original price and the quantity being purchased
* @param $context
* An array containing details about how the price will be used. The context typically contains:
* revision: Which is one of  original, altered, formatted-original, formatted, themed-original, themed. This value is not used by this function as all prices are first altered before determining how they will be displayed.
* type: Which type of price it is. This function handles the following types:
* - order_product: The product (and it's price) is part of an order
* - cart_item: The product is in the cart
* - product: Just displaying the product on a page.
* subject: Contains data about the thing that the price is being applied to. Depending on the type, this will be different.
* - order_product: expects order and node
* - cart_item: expects node
* - product: expects node
*/
function test_additional_license_alter(&$price_info, &$context, &$options){
// Make sure we are dealing with one of the products, it has a set additional license fee and the qty in cart is greater then 1
if($context["type"] == "cart_item" && isset($context["subject"]["node"]->field_product_additional_license[0]) && $context["subject"]["node"]->field_product_additional_license[0]["value"] > 0.00 && $price_info["qty"] > 1){
$first_fee = $context["subject"]["node"]->sell_price;

$additional_fees = $context["subject"]["node"]->field_product_additional_license[0]["value"] * ($price_info["qty"] - 1);

// Now create the unit per qty price to be used for the ubercart calculations
$price_info["price"] = round(($first_fee + $additional_fees) / $price_info["qty"], 2);
$context['revision'] = 'altered';
}
}

Anybody able to provide some feedback if there is something missing to actually make the price handler work properly throughout the entire Checkout and Order Management systems?

agrah's picture
Offline
Joined: 12/10/2011
Juice: 22
Re: Hey pferlito, Did you get the

Anyone have any clue how to do this in Ubercart 3. hook_uc_price_handler() has been removed for some reason.