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.
- 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).
- 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".
- 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).
- Create a product
- 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.
Comments
Thank you.
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. :)
Custom line item types and displaying products using a view
Hi, Randy - how would you go about associating a custom line item type with a product that you display using a view rather than a product display node?
Thanks!
Lee
Add new comment