0

I need to temporarily remove a few line items from an order. I can remove them well enough, but they won't add back. There aren't any error messages in watchdog, the error log, or on screen.

Here is the code I have currently:

$orderWrapper = entity_metadata_wrapper('commerce_order', $order);
$removedLineItems = array();

// Loop over each line item on the order.
foreach ($orderWrapper->commerce_line_items as $delta => $lineItemWrapper)
{
  $product = $lineItemWrapper->commerce_product->value(); // Get id to pass to line_item_delete

  if ($product->some_field[LANGUAGE_NONE][0]['value'] !== 'some condition')
  {
    $lineItem = commerce_line_item_load($lineItemWrapper->getIdentifier());
    $removedLineItems[] = $lineItem;

    commerce_line_item_delete($lineItemWrapper->getIdentifier());
    unset($orderWrapper->commerce_line_items[$delta]);
  }
}
commerce_order_save($order);


// Run some other code on $order
somePlugin_someFunc($order);


// Add back items removed for package calculation
if (count($removedLineItems) > 0)
{
  foreach ($removedLineItems as $lineItem)
  {
    commerce_line_item_save($lineItem);

    $orderWrapper->commerce_line_items[] = $lineItem;
  }

  commerce_order_save($order);
}

Anyone have an alterative to the above?

0

Turned out the issue was simpler than I thought. If you think about the field between line_items and orders as a reference, it makes sense to just remove that reference, save the order, than re-add it later. I assume the issue was arising that Drupal didn't like deleting than recreating the exact same line_item, so if it isn't ever deleted, than the reference can just be updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.