[Drupal] How to create a custom line item type programatically?

| | 1 min read

In one of my recent projects i came across a situation where i had to create a order line item programatically. If you have ever came across such a situation you would end up in line_item_example.module which demonstrate the addition of a new line item type programatically.

I followed the steps specified in the line item example module but the commerce_unit_price and commerce_total fields were not added to my line item.

On further debugging i found out that this happened as my line item was created in a disabled status

In order to enable my line item i have to call the function,

  commerce_line_item_configure_line_item_fields(array('module_which_defines_line_item'));

This function is called whenever a module is enabled. As i created my line item in a custom module which was already enabled, this function was not called and my line item was in disabled status

Hence i wrote a update hook and run the above function

My line item is enabled. Hope this helps some one