5 replies [Last post]
florissantti's picture
Offline
Joined: 08/27/2009
Juice: 22
Was this information Helpful?

Before 2.0 you could get away with having line items with ids that did not match the id defined in hook_line_item. I could have say:

function my_module_line_item() {
  $items = array();
  $items[] = array (
    'id' => 'kit_discounts',
  ...
}

callback_from_above($op, $order) {
  ...
  drupal_add_js("Drupal.behaviors.ucDiscountMods = function (context) { set_line_item('kit_discounts_1', '". $title ."', ". $value .", $weight); } ", 'inline');
  drupal_add_js("Drupal.behaviors.ucDiscountMods = function (context) { set_line_item('kit_discounts_2', '". $title ."', ". $value .", $weight); } ", 'inline');
  drupal_add_js("Drupal.behaviors.ucDiscountMods = function (context) { set_line_item('kit_discounts_3', '". $title ."', ". $value .", $weight); } ", 'inline');
  ...
}

Now you cannot. Each line item must be associated with its own, unique hook_line_item function and their ids must match.

This is what I gather so far, anyhow.

I have a small problem. I have a module that is written pretty thoroughly into the old way. No matter which way I look at it I cannot figure out a way to get around this without a lot of recoding.

Am I fully correct in my understanding here? Is there a way around this? Is this new feature/fix just that or a mistake?

I appreciate any advice.

Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6846
Re: new hook_line_item requirements in 2.0

I'm not sure what would have changed that prevents you from doing that. The tax module works in a similar way, at least in the Javascript side for the payment preview pane. What particular part about the line item API keeps you from doing what you want?

florissantti's picture
Offline
Joined: 08/27/2009
Juice: 22
Re: Re: new hook_line_item requirements in 2.0

Yes, I actually built my code around how uc_taxes.js handled adding line items.

I went through and sure enough, uc_taxes has no trouble adding multiple lines and the (set_line_item) ids (or keys as they are in code) are set up similarly to what I have.

It looks like the 'Order total' line item is not adding up values correctly. I've attached a screenshot.

I have a similar but different discount module that just puts out a single line item and uses for its id the id given in hook_line_item and it calculates just fine.

AttachmentSize
order-total.jpg 52.99 KB
Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6846
Re: Re: Re: new hook_line_item requirements in 2.0

What's the rest of your hook_line_item() look like? I still have trouble sometimes remember what all of those flags do. However, it looks like you need to set 'calculated' to TRUE because that tells Ubercart to add that line item into the grand total. I would think that the subtotal without taxes would respect that setting the same way, though.

florissantti's picture
Offline
Joined: 08/27/2009
Juice: 22
Re: Re: Re: Re: new hook_line_item requirements in 2.0

Yeah, I almost mentioned that I checked my 'calculate' value 20 times and even switched it back and forth just to make sure I wasn't backwards. I also started to play with the mysterious, undocumented 5th and 6th flags of set_line_item in hope. Summed either 1 or 0 and some unknown boolean. Seemingly no combo of those gets the order total to work.

Thanks a lot for looking into this. This site is supposed to launch tomorrow and I just noticed that this stopped working last night...

/**
* Implementation of hook_line_item().
*/
function uc_discount_mods_line_item() {
  $items = array();
  $items[] = array(
    'id' => 'pkdiscounts',
    'title' => t('Discount'),
    'callback' => 'uc_discount_mods_line_items',
    'weight' => 5,
    'stored' => TRUE,
    'default' => FALSE,
    'calculated' => TRUE,
    'display_only' => FALSE,
  );

  return $items;
}

function uc_discount_mods_line_items($op, $order) {
  switch ($op) {      
    case 'cart-preview':
    $discounts = uc_discount_mods_calculate_discounts($order);
      if ($discounts == NULL)
        break;
      $unique_id = 100;
      foreach ($discounts as $line_item) {
        $title = $line_item['title'];
        $value = $line_item['value'];
        $weight = $line_item['weight'];
        $unique_id++;
                drupal_add_js("\$(document).ready( function() { set_line_item('". $unique_id ."', '". $title ."', ". $value .", $weight, 1, false); } );", 'inline');
}
  break;
 
    case 'load':
      $discounts = uc_discount_mods_calculate_discounts($order->products);
      if ($discounts == NULL)
        break;
      $lines = array();
      $unique_id = 100;
        foreach ($discounts as $line_item) {
          $unique_id++;
          $lines[] = array(
            'id' => $unique_id,
            'title' => $line_item['title'],
            'amount' => $line_item['value'],
          );
        }
        return $lines;
  }
}

florissantti's picture
Offline
Joined: 08/27/2009
Juice: 22
Re: Re: Re: Re: Re: new hook_line_item requirements in 2.0

I'm still very much working on this. If anyone has any idea what could be wrong I'd greatly appreciate it. It's been a rather frustrating week with regard to this module.