Community Documentation

Adding new line item types

Last updated October 21, 2011. Created by sreynen on September 21, 2011.
Edited by jazzdrive3. Log in to edit this page.

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;

}

Then, 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('custom_type');

}

Page status

About this page

Drupal version
Drupal 7.x
Audience
Programmers
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here