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

What is the correct way to delete line items?

I have some line item fields on orders that reference line items that don't exist anymore. I reported this in #2124225.

rszrama pointed me in the direction of looking for instances where line items were deleted without calling commerce_line_item_delete_references(). Sure enough I found this in one of my custom modules:

<?php
// Remove the discount
$discount_amount = $line_item_wrapper->free_upgrade->commerce_price->amount->value() * $quantity * -1;
foreach(
$order_wrapper->commerce_line_items as $item_wrapper){
  if(
$item_wrapper->commerce_unit_price->amount->value() == $discount_amount){
   
// Good chance this is the discount line item
   
commerce_line_item_delete($item_wrapper->line_item_id->value());
    break;
  }
}

// Remove the free upgrade
foreach($order_wrapper->commerce_line_items as $item_wrapper){
  if(
$item_wrapper->commerce_product->product_id->value() == $line_item_wrapper->free_upgrade->product_id->value()){
   
// This is the free upgrade
   
commerce_line_item_delete($item_wrapper->line_item_id->value());
    break;
  }
}
?>

From rszrama's comment on my previous issue, I understand that the correct way to do this would be something more like:

<?php
// Remove the discount
$discount_amount = $line_item_wrapper->free_upgrade->commerce_price->amount->value() * $quantity * -1;
foreach(
$order_wrapper->commerce_line_items as $item_wrapper){
  if(
$item_wrapper->commerce_unit_price->amount->value() == $discount_amount){
   
// Good chance this is the discount line item
   
commerce_line_item_delete_references($item_wrapper->value());
   
commerce_line_item_delete($item_wrapper->line_item_id->value());
    break;
  }
}

// Remove the free upgrade
foreach($order_wrapper->commerce_line_items as $item_wrapper){
  if(
$item_wrapper->commerce_product->product_id->value() == $line_item_wrapper->free_upgrade->product_id->value()){
   
// This is the free upgrade
   
commerce_line_item_delete_references($item_wrapper->value());
   
commerce_line_item_delete($item_wrapper->line_item_id->value());
    break;
  }
}
?>

But I am unable to find documentation to confirm that.

Can anyone tell me if this is right? Would a $item_wrapper->delete(); accomplish both deleting the line item and the references to it?

Thanks!

Asked by: bander2
on February 24, 2014

1 Answer

Vote up!
0
Vote down!

I'd like to bump this. It is still an open question for me.

Answer by: bander2
Posted: Apr 15, 2014