0

I've managed to get new line items added to a recurring order using hook_commerce_license_billing_order_refresh_alter() and the code is:

$product = commerce_product_load_by_sku($sku);
$li = commerce_product_line_item_new($product, '1');
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_needs_save = TRUE;
commerce_line_item_save($li);
$line_items[$li->line_item_id] = $li;

It adds line items and correctly calculates new total and shows on the recurring order page. However, since this is a refresh hook it keeps creating new products, line items and licenses on every refresh. So I tried to attach already created line items the order's data array:

// Add line item objects to order data.
$order->data['lis'][] = $li;
// Add sku to order data.
$order->data['skus'][] = $sku;

and then re-use it with:

if (!empty($order->data['lis'])) {
        foreach ($order->data['lis'] as $key => $li) {
          $wrapper = entity_metadata_wrapper('commerce_order', $order);
          $order_needs_save = TRUE;
          $line_items[$li->line_item_id] = $li;
        }
      }

but, unfortunately, the new line items do not show on the recurring order. If I add commerce_line_item_save($li); line, then the website crashes giving:

EntityMetadataWrapperException: Unable to get the data property line_item_id as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue() (line 457 of /home/username/public_html/sites/all/modules/entity/includes/entity.wrapper.inc).

So my question is: how I should properly add new line items to existing recurring order?

3
  • Just to keep everything at one place, here is the quote from Boyan on the subject: The recurring order only has recurring line items, generated from the active licenses.It is auto-generated and auto-updated, so no manual updates are allowed, and certainly not with non-recurring products. on drupal.org/project/commerce_license_billing/issues/2307917
    – Nick
    Mar 6, 2018 at 13:54
  • ...Drupal 7 or 8?
    – No Sssweat
    Mar 11, 2018 at 9:07
  • @NoSssweat the above function and code gives it's Drupal 7. Anyway, the above code kind of works, however the line item never get's really added to the recurring order. If you go this path then you have to always re-add the new line item with refresh function. So I don't think this is a good way anymore.
    – Nick
    Mar 11, 2018 at 13:37

1 Answer 1

1
+50

It works even with:

if (!empty($order->data['lis'])) {
  foreach ($order->data['lis'] as $key => $li) {
    $line_items[$li->line_item_id] = $li;
  }
}

However, to avoid re-adding new line item every recurring cycle, the hook_commerce_order_update should be used instead as recommended on https://cgit.drupalcode.org/commerce_license_billing/tree/commerce_license_billing.api.php#n129:

Modules that need to respond to a license plan or recurring order changing should use hook_commerce_order_update instead.

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.