Discussions

Alter cart unit price and total amount

I need help on this code which is invoked on calculate sell price rules event on cart view. Line items and order are properly modified. But nothing is displayed, old unit price amount and old total amount are displayed. Even after cart refresh. Any idea ?

                                                                     
/**                                                                            
* Calculates the price when line item are dispalyed
* or created.                 
*/              

function mymodule_calculate_product_price_cart($order) {               
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);          
    // Iterates on line items and alter the price.                               
    $commerce_line_items = array();                                              
    foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
      $line_item = $line_item_wrapper->value();                                  
      $type = $line_item_wrapper->commerce_product->value()->type;               
      if ($type === 'insurance') {                                               
        $package_price   = $line_item_wrapper->commerce_product->field_package_ref->commerce_price->amount->value();
        $warranty_price  = $line_item_wrapper->commerce_product->commerce_price->amount->value();
        $amount = _mymodule_calculate_policy_price($warranty_price, $package_price);
      }                                                                          
      elseif ($type === 'little_product') {                                      
        $amount = $line_item_wrapper->commerce_product->commerce_price->amount->value();
      }                                                                          
      else {                                                                     
        $commerce_line_items[$delta] = $line_item;                               
        continue;                                                                
      }                                                                          
      $qty = $line_item->quantity;                                               
      // This sets the unit price by adding a component price 'maliceo_price'       
      mymodule_line_item_price_amount($line_item, $amount, 'commerce_unit_price');
      mymodule_line_item_price_amount($line_item, $qty * $amount, 'commerce_total');
      commerce_line_item_save($line_item);                                       
      entity_get_controller('commerce_line_item')->resetCache(array($line_item->line_item_id));
      $commerce_line_items[$delta] = $line_item;                                 
    }                                                                            
    $order_wrapper->commerce_line_items = $commerce_line_items;                  
    module_invoke_all('commerce_cart_order_refresh', $order_wrapper);            
    commerce_order_save($order_wrapper->value());                                
  } 
Posted: Oct 10, 2012