How to add a new line item to existing recurring order? 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-&gt;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-&gt;data['lis'][] = $li;
// Add sku to order data.
$order-&gt;data['skus'][] = $sku;


and then re-use it with:

if (!empty($order-&gt;data['lis'])) {
        foreach ($order-&gt;data['lis'] as $key =&gt; $li) {
          $wrapper = entity_metadata_wrapper('commerce_order', $order);
          $order_needs_save = TRUE;
          $line_items[$li-&gt;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-&gt;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?
It works even with:

if (!empty($order-&gt;data['lis'])) {
  foreach ($order-&gt;data['lis'] as $key =&gt; $li) {
    $line_items[$li-&gt;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.