Alter a line-item field

Public

Below the code how to programmatically change fields of a line item.
The example targets only the line-item type "product", remove the if if not needed

</> CopyGet raw version
php
  1. // Process line items and set Custom field
  2. $line_items = $order_wrapper->commerce_line_items;
  3. foreach ($line_items as $line_item_wrapper) {
  4. // Get Product line items only
  5. if ($line_item_wrapper->type->value() == 'product') {
  6.  
  7. // Set custom line item field example
  8. $_custom_field_val = $delivery_date_confirmed;
  9. $line_item_wrapper->field_custom->set($_custom_field_val);
  10.  
  11. // Save the line item updated information.
  12. $line_item_wrapper->save();
  13.  
  14. }
  15. }