Introduction to Custom Line Items with Drupal Commerce

Custom Line Item Types were a late addition to the Drupal Commerce store technology suite. From the beginning, we've had custom product types, which could have any number of custom fields. These were great when the store defines all the information about the products (like red-small-t-shirts and blue-medium-t-shirts) but don't provide enough power when the customer needs to define something about the product. Enter custom line item types, where the store-builder can add any type of field to a line item and the end-user can configure or provide notes or even prices for a product being added to their cart.

In this screencast I give step-by-step instructions on how to add fields to line items.

In the simplest situation just add a field or fields to the default line item type and configure each as exposed to the add-to-cart field.

  1. Add a field to a line item type (either a custom line item type or the default one). Visit admin/commerce/config/line-items and use "Manage fields" to add field(s).
  2. Configure the field to be used on add-to-cart forms: Select the checkbox to "Include this field on Add to Cart forms for line items of this type".
  3. Configure the product reference field to use the correct line item type. On the content type that will be used to display the product, configure the display settings of the product reference field to use the correct line item type (unless you only have one line item type).
  4. Create a product
  5. Try it out

The next step up is to add a custom line item instead of using the default line item type.

This requires you to use a module that will provide custom line item types like Commerce Custom Line Items or custom code based on a template like the Line Item Example in Commerce Examples.

After that, though, everything is the same. You

  • Create a line item type
  • Add fields to it
  • Tell the fields that they're to be presented on the add-to-cart form
  • Create a product
  • Create a product display content type
  • Add a product reference field to it
  • In the display settings, configure the add-to-cart field to use the new custom line item type.
  • Create a node of the new type
  • Done.

Here's the screencast showing these two approaches.

Posted November 1, 2011

Comments

Submitted by Michael Favia (not verified) on

Thank you randy i needed to check on this for a new client site and your article and walkthrough were perfect for allying my fears that id have to write this functionality myself. :)

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <br>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.