Currently the only event that reacts on order deletion is "After deleting a commerce order.".

However, if you need to do some tasks depending on the orders line items properties you can't do this because at this time the line items are already deleted. The $order object passed to the event lists the line items ids but you can't load the line items anymore.

So there there needs to be a hook/event that fires before they get deleted.

In the meantime - is there a workaround?

Comments

rszrama’s picture

Status: Active » Closed (won't fix)

I don't see us adding such a hook specifically for orders; right now the hooks / events we use are pretty standard for entities across all Drupal modules. Depending on what you're trying to do, I'd either hook into hook_commerce_line_item_delete() or implement my own hook_field_attach_delete() in a custom module weighted to execute before the commerce_line_item.module's implementation. Referenced line items get deleted on that hook, which receives the full original referencing entity as well, so you can do what you need based on the order being deleted and the referenced line items.

See commerce_line_item_field_attach_delete() for more info.

haggins’s picture

Thank you! Sounds good, I will loook into this.

edit: hook_field_attach_delete() is exactly what I need. Thanks for that idea! This saved my day.

ThaJoa’s picture

Hi

I need something like this too.
I want to remove a role from the user who owned the order. I need to read out the user and the product sku for this.

How did you do this? What does your hook look like?

haggins’s picture

Just as an example:

function mymodule_field_attach_delete($entity_type, $entity) {
  if ($entity_type == 'commerce_order') {
    $order_wrapper = entity_metadata_wrapper('commerce_order', $entity);

    $line_items = $order_wrapper->commerce_line_items->value();
    foreach ($line_items as $line_item) {
      if ($line_item->type == 'myCustomType') {
        mymodule_some_function($line_item, -$line_item->quantity);
      }
    }
  }
}

I recommend reading this article if you are not familiar with entity_metadata_wrapper.