Lots of people have been asking about how to do donations in Drupal Commerce, and since I've been exploring custom line item types, it seemed like that would be a reasonable way to do it.
This screencast is based on the previous Introduction to Custom Line Item Types, so if you're not familiar with the basic concepts try that one.
Here we're going to use the basic techniques described there, plus a neat little trick with a rule, to let the end-user tell us how much they want to contribute, instead of just providing a set of products with predefined values.
We'll be using Commerce Custom Line Items and Select or Other in this example, but no custom code.
- Create a custom line item type using Commerce Custom Line Items or another module that provides line item types.
- Add a regular integer field to the line item type.
- Choose one of the "Select or other" widgets for the line item type.
- Create a content type with a product reference field.
- In the display settings for the product reference field's add-to-cart, choose the custom line item as the line item to use.
- Create a product.
- Create a piece of content of the type we created and add the product to it.
At this point you'll have an add-to-cart field and the user will be prompted for the amount they want to donate. We just need to have that amount reflected in the price now, so we'll use a product pricing rule:
-
Condition: entity has field: field_donation_amount
-
Action: Calculate field_donation_amount * 100
- Action: Line item: Set the unit price to value: calculated amount
I'll attach the rule to this article.
Here's the screencast:
I promised a features feature to demonstrate this, but the Commerce Features module is not quite ready for line items yet (and would have required a patch anyway). But attached is an export of the rule I described here.
Comments
Thanks for the screencast! If
Thanks for the screencast! If you're like me and take things too literally, please note that at 9:00 you probably need to add the product reference as a new field, not an existing one . :-)
Display node title referenced
Hi there!
We're developping a charity website. For this purpose, we followed your genius screencast.
We've been facing an annoying issue with the shopping cart views when using "Relationship: Content: Referenced Product" to retrieve the title of the node a donation is referenced to.
Basically, after making a donation, we're viewing our cart block but the shopping cart is listing every node containing our donation product reference and we're stuck with that.
Any idea on how to fix this because we're trying to solve this issue for weeks now?
Add new comment