Commerce License Billing module has the following function, which automatically deletes corresponding billing cycle if recurring order gets deleted:
function commerce_license_billing_commerce_order_delete($ order) { if ($ order->type == 'recurring') { $ order_wrapper = entity_metadata_wrapper('commerce_order', $ order); $ order_wrapper->cl_billing_cycle->delete(); } }
which is good to keep database clean from zombie billing cycles.
However, the problem is that licenses for the deleted order’s products keep the same Active
status when they were supposed to get Revoked
. So I tried to write a new function in similar manner:
function my_module_entity_delete($ entity, $ type) { if ($ type == 'commerce_order') { if ($ entity->status == 'recurring_open') { foreach ($ entity->commerce_line_items[LANGUAGE_NONE] as $ key => $ value) { $ line_item_id = $ value['line_item_id']; dpm($ line_item_id); $ line_item = commerce_line_item_load($ line_item_id); dpm($ line_item); if (isset($ line_item->cl_billing_license[LANGUAGE_NONE][0]['target_id'])) { $ license_id = $ line_item->cl_billing_license[LANGUAGE_NONE][0]['target_id']; $ license = entity_load('commerce_license', array($ license_id)); $ license[$ license_id]->status = '4'; entity_save('commerce_license', $ license[$ license_id]); } } } } }
Unfortunately, it didn’t work, because while dpm($ line_item_id)
correctly returns the line item id dpm($ line_item)
never returns empty. Apparently the corresponding line item already gets deleted by that time, so the function is not able to get to its license.
Is there any hook that enables to catch the licenses for the deleted order? Maybe there is a better way of achieving the same goal – to change the status of relevant licenses if recurring order is deleted?