Tags/topics: 
1
Answers
Vote up!
0
Vote down!

updating the shipping line item after rules have run

I have a shop where the shipping price calculation is so complicated i need to basically add an extra amount after rules calculation is done. I added an extra rule that should allow me to do this, but any time I try to alter the line item, it gets overwritten right after i call any sort of save function.

Is there any way of making this work?

<?php
 
function custom_add_extra_shipping($order,$amount){
   
dsm('rule: add an extra shipping amount');
   
$amount = 5000; //this is hardcoded, but in my code it's part of a huge calculation

   
foreach ($order->commerce_line_items['und'] as $id) {
       
$line_item = commerce_line_item_load($id['line_item_id']);
        if(
$line_item->type == 'shipping'){
           
dsm($line_item);
           
$line_item->commerce_total['und'][0]['amount'] = $amount;
           
$line_item->commerce_total['und'][0]['data']['components'][0]['price']['amount'] = $amount;
           
           
commerce_line_item_save($line_item);
           
        }
    }
}
?>
Asked by: jonne.freebase
on September 24, 2012

Comments

Is there any reason you don't just do this calculation in a rate calculation rule weighted to run at the end of the process?

- Ryan Szrama on September 25, 2012

Ryan -- that's what I'm trying to do but I can't get the action to work. Trying to use 'Set a data value'.

- MrPeanut on October 29, 2012

1 Answer

Vote up!
1
Vote down!

The recommended route is to use the Rules interface. Ask a more rules specific question for additional help.

Josh Miller
Answer by: Josh Miller
Posted: Nov 16, 2012