Drupal 7: Programatically update commerec Line Item with example

In programmatically we can able to alter commerce line items. The line items are kept as an entity in Drupal commerce system. Using the function "commerce_line_item_load()" we load line item as the object and able to access line item properties. The function commerce_line_item_save() will help to update the line item.


$lineItemObj = commerce_line_item_load(LINE_ITEM_ID); // Loading Line item.
$lineItemObj->FIELD_1 = FIELD 1 VALUE; // New value for Field 1
$lineItemObj->FIELD_2 = FIELD 2 VALUE; // New value for Field 2
commerce_line_item_save($lineItemObj); // Updating Line Item.


$lineItemObj = commerce_line_item_load(831583);
$lineItemObj->commerce_unit_price['und'][0]['data']['components'][0]['price']['amount'] = 4500;
$lineItemObj->commerce_unit_price['und'][0]['data']['components'][1]['price']['amount'] = -500;