Most line items are products, but line items can be different types, e.g. coupons, discounts, shipping, tax, etc.

Line item types must first be defined with hook_commerce_line_item_type_info(), e.g.:

To create another product line item type (for instance, if you need to add additional fields that will be available on the Add to Cart form), use the following to essentially duplicate the default line item type and then build off of it.

/**
 * Implements hook_commerce_line_item_type_info().
 */

function custom_type_commerce_line_item_type_info() {
	return array(
		'custom_type' => array(
			'name' => t('Custom Type'),
			'product' => TRUE,
			'description' => t('References a product.'),
			'add_form_submit_value' => t('Custom Text'),
			'base' => 'commerce_product_line_item',
		),
	);
}

To implement a completely custom line item type that will not reference a product type, use something like below. This can be used for things like coupons, shipping, etc. Anything you want to add to an order that will affect its total.

/**
 * Implements hook_commerce_line_item_type_info().
 */
function custom_type_commerce_line_item_type_info() {

  $line_item_types = array();

  $line_item_types['custom_type'] = array(
    'name' => t('A custom type'),
    'description' => t('This custom type is for custom line items.'),
    'product' => FALSE,
    'base' => 'custom_type',
  );
  
  return $line_item_types;

}

If you are implementing a product line item type, in hook_enable, it is necessary to tell commerce_line_item that standard fields (e.g. price) must be added to the new type:

/**
 * Implements hook_enable().
 */
function custom_type_enable() {

  commerce_product_line_item_configuration(array('type'=>'custom_type'));

}

If you are creating a custom line item type that does not use commerce_product_line_item as it's base, you instead need to do the following. This adds standard fields such as commerce_unit_price, and commerce_total.

/**
 * Implements hook_enable().
 */
function custom_type_enable() {

     commerce_line_item_configure_line_item_fields(array('custom_type'));

}

Note that the parameter to that function is an array of module names to check for custom line items, so just pass in the name of your module, not the type of the line item you created, unless they happen to be the same (as they are in this sample).

Comments

Murz’s picture

On my site function

  commerce_product_line_item_configuration(array('type'=>'custom_type'));

works, but it creates only 2 fields: commerce_display_path, commerce_product
but none of commerce_unit_price, commerce_total that I needed.

Why this happens and how I can fix this problem?
Commerce module version is 7.x-1.9.

Murz’s picture

Second function commerce_line_item_configure_line_item_fields(array('mymodulename')); solves this problem.

Bits8myBytes’s picture

I ran into a similar problem and fixed it. One thing that is not mentioned is that if you make any changes to the enable and line item configuration functions you need to reinstall the custom module because some of these functions are only run when you first enable a module.

rwilson0429’s picture

For those who do not want to write code, there are commerce modules (i.e. commerce_coupons) that perform these tasks very well.

ReggieW