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.
- 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.