Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Dynamically putting select values on a line item in Drupal Commerce

Parent Feed: 

As I wrote earlier, I am creating a registration system using Drupal Commerce for Vejle Idrætshøjskole. For one of our course type, we need the customer to be able to select subjects during the checkout process. We decided to do it on a custom line item using custom customizable products where we added the subjects as a field.

The subjects is not the same on all of our courses on that course type, so I attached the subjects using an entityreference. Using the entityreference module under the settings for the field, I can use Views: Filter by an entityreference view to select the entities, I want to show for for the entityreference field on the custom line item on the add to cart form.

However, when using the add to cart form, I could not simply choose the current page node id for the contextual filter for the referenced entities. Instead I had to use a token to make the reference from the currently chosen product to the course display content type with the referenced subjects.

Luckily, that is possible using the patch from the issue: Dynamic Views Arguments Using Tokens. Now I was able via the settings page for the subjects field on the custom line item type to supply an argument for my view using this token: [commerce-line-item:commerce-product:product-id].

When I resolved this issue issue everything worked beautifully. How do you handle registrations with Drupal Commerce?

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web