Using Custom Line Items To Provide a Donation Feature to Drupal Commerce

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.

  1. Create a custom line item type using Commerce Custom Line Items or another module that provides line item types.
  2. Add a regular integer field to the line item type.
  3. Choose one of the "Select or other" widgets for the line item type.
  4. Create a content type with a product reference field.
  5. In the display settings for the product reference field's add-to-cart, choose the custom line item as the line item to use.
  6. Create a product.
  7. 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.

Posted November 1, 2011

Comments

Submitted by peezy (not verified) on

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

Submitted by Donatoo (not verified) on

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

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.
Type the characters you see in this picture.
Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.