hook_line_item

Posts: 110
Joined: 08/08/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.Not Kulvik

Iam trying to write a small discounts module for our system.

This is what Iam doing

<?php
function uc_vingodiscounts_line_item(){
       
$items[] = array(
               
'id'=>'vingodisc',
               
'title'=>t('Vingo Discounts'),
               
'weight'=>4,
               
'default'=>TRUE,
               
'stored'=>TRUE,
               
'add_list'=>TRUE,
               
'calculated'=>TRUE,
               
'display_only'=>FALSE,
               
'callback'=>'uc_vingodiscounts_discount'
       
);
        return
$items;
}


function
uc_vingodiscounts_discount($op,$order){
       
//$discounts = uc_vingodiscounts_calculate($order);
       
switch ($op){
                case
'cart-preview':
                       
$script = "set_line_item('vingodisc','Vingo Discount1','-5.00');\n";
                        if (
$script){
                               
drupal_add_js("\$(document).ready( function() { ". $script ." } );", 'inline');
                        }
                        break;
                 case
'load':
                       
$lines[] = array(
                              
'id' => 'vingodisc',
                              
'title' => 'Vingo Discount1',
                              
'amount' => -5.00,
                        );
                        return
$lines;
                        break;
        }
}                                                    
?>

I see the discount in the checkout page. But when i review my order it seems to still not discount it. Where Am i going wrong?

Posts: 110
Joined: 08/08/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.Not Kulvik

Anyone knows whats happening ?

Posts: 50
Joined: 08/08/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.Not Kulvik

Because you are only doing the discount for the checkout page Smiling

You have to hook into hook_order too.

function uc_simpleflatrate_order($op, &$arg1, $arg2) {
  switch ($op) {
    case 'total':
      # this is code block for the order total
      $amount = 0;
      foreach($arg1->products as $product) {
        $amount -= 5; # subtract 5 for each product
      }
      return $amount; # I think a negativ value should be possible here
      break;
    case 'save':
      # order save block
      $amount = 0;
      foreach($arg1->products as $product) {
        $amount -= 5; # subtract 5 each product
      }
      db_query("DELETE FROM {uc_order_line_items} WHERE order_id = %d AND type = '%s'", $arg1->order_id, 'discount');
      uc_order_line_item_add($arg1->order_id,
                               'discount',
                               t('Total discount'),
                               $amount,
                               3);
           
      break;
  }
}

This is some really simplified code... But I hope you get the general idea for how it works Smiling

--

Erlend Strømsvik
Ny Media AS
erlend@nymedia.no

Posts: 110
Joined: 08/08/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.Not Kulvik

Appreciate the help...

Thanks

Posts: 11
Joined: 08/09/2007
Cool profile pic award.

quaoar, the information you posted here was very helpful for me to finish up the core of the Discounts module.

cosmo, have you considered the Discounts module? It should be ready for general testing now, and it's designed to be modular, so people can easily add their own discounts.

Posts: 110
Joined: 08/08/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.Not Kulvik

Actually i have tried urs but found that it had the same problem as mine, and that is when i posted this. Let me try ur module now and give u an update soon.

Posts: 195
Joined: 08/13/2007
Cool profile pic award.Getting busy with the Ubercode.Internationalizationizer

I have the same problem in a similar situation, adding a value based on the payment method choice.
Please post if it's solved.

Kees

Posts: 195
Joined: 08/13/2007
Cool profile pic award.Getting busy with the Ubercode.Internationalizationizer

Never mind,

hook_order IS the solution. The returned value is'nt the total, but added to the total. Simple, isnt't it?
(once you know it)

Posts: 110
Joined: 08/08/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.Not Kulvik

It is hook_order_item.

I have taken a look into the discounts module. I needed much simpler. My condition is this. If i add more than 12 items (Or a bottle case), we discount 10% on items which are not on sale (list_price != sell_price)

<?php
function uc_vingodiscounts_line_item(){
       
$items[] = array(
               
'id'=>'vingodisc',
               
'title'=>t('Vingo Discounts'),
               
'weight'=>4,
               
'default'=>TRUE,
               
'stored'=>TRUE,
               
'add_list'=>TRUE,
               
'calculated'=>TRUE,
               
'display_only'=>FALSE,
               
'callback'=>'uc_vingodiscounts_discount'
       
);
        return
$items;
}


function
uc_vingodiscounts_discount($op,$order){
       
$discounts = uc_vingodiscounts_calculate($order);
        switch (
$op){
                case
'cart-preview':
                       
$script='if(set_line_item != undefined){';
                       
$discountnum=0;
                        foreach(
$discounts as $discount){
                               
$script .= "set_line_item('vingodisc".$discountnum."','".$discount['title']."','".$discount['amount']."');\n";
                               
$discountnum++;
                        }
                       
$script .= "}";
                        if (
$script){
                               
drupal_add_js("\$(document).ready( function() { ". $script ." } );", 'inline');
                        }
                        break;
        }

}


function
uc_vingodiscounts_calculate($order){
       
$discounts=array();
        foreach(
$order as $orderitems){
               
$discount=array();
               
$node=node_load($orderitems->nid);
               
$query="SELECT * from {term_node} where nid=".$node->nid." and tid=1";
               
$res=db_query($query);
                if((
$node->list_price == $node->sell_price) && db_num_rows($res)==1){ // Apply 10% discount
                       
$discount['title']="Discount 10% - ". $node->title;
                       
$discount['amount']=($orderitems->price*$orderitems->qty) * -0.10 ;
                       
$discounts[]=$discount;
                }
        }
        return
$discounts;
}


function
uc_vingodiscounts_order($op,&$arg1,&$arg2){
       
$discounts=uc_vingodiscounts_calculate($arg1->products);
        switch(
$op){
                case
'total':
                       
$amount=0;
                        foreach(
$discounts as $discount){
                               
$amount += $discount['amount'];
                        }
                        return
$amount;
                        break;
                case
'save':
                       
$discountnum=0;
                       
db_query("DELETE FROM {uc_order_line_items} WHERE order_id = %d AND type LIKE '%vingodisc%'", $arg1->order_id);
                        foreach(
$discounts as $discount){
                               
uc_order_line_item_add($arg1->order_id,'vingodisc'.$discountnum,$discount['title'],$discount['amount']);
                               
$discountnum++;
                        }
                        break;

        }
}
?>