Implementing a donation system with Drupal Commerce

Implementing a donation system with Drupal Commerce

By Vasi Chindris 19th December 2012 Drupal

The Drupal Commerce module is a great solution for anyone who wants to have a shop implemented with Drupal. It doesn't matter if you configure the shop from scratch or if you use an installation profile, you can have a shop in a very short time. Most of the time is just configuration, almost no coding required.

By default, you can create your products, set the prices for them, categorize them, create rules for tax handling, use different payment gateways (also with the help of some contributed modules) and many many other things. Unfortunately there is one thing you cannot do just with configuration: a donation system. The main problem in the donation system is that the amount of each donation can be different. If your donation system has only fixed amounts, then there is no problem, all you have to do is to create one product for each donation amount. But when the amount is variable, then implementing this using only configuration may not be so trivial (if not possible at all).

I will explain next how we deal with this case, when we have to build such platforms.

The concept

If we go a bit deeper and check how the Drupal commerce system is built and how the price of the order is calculated, we can see that each line item from the order has a unit price field, that usually is populated with the price of the product to which it is associated. So the user clicks on the button to add the product to the cart, and this triggers the creation of a line item in the order that will have the unit price of that product. But in our case, the price of the product is variable, because it is about a custom amount that the user wants to donate, so we cannot really use that price to populate the line item. We will have to handle this by ourselves, to implement some logic that will populate the unit price of the line item with the amount of the donation. The solution involves site configuration, as well as some coding.

The main idea is to have a new field on the line items (a price field) that will be populated with the value the user wants to donate, and then using a rule that will be triggered on the event “Calculating the sell price of a product” to put the value from our custom field into the price field of the line item. The reason why we do not just put the value directly in the line item price field, when we create the line item, is that the price of the line item can actually be updated later. For example commerce_cart_order_refresh() would update the price of the line items. In this case, our code that populates the unit price will not be executed, and we will not be able to maintain the correct price of the line item. That's why we use that rule that fires on the “Calculating the sell price of a product” and we store the amount that is being donated in a separate field that will not be altered by any other code.

The setup

And this is how it works:

add a price field on the line item type (admin/commerce/config/line-items/product/fields), with the name: field_variable_price

create a product with the SKU “donation” that has the price set to 0

add a rule that updates the price of the line item


 

For the coding part, an example can be found in a sandbox project. An important remark for the coding part is that we actually have to create the order and add the donation product to the order by ourselves, because we have to populate the field_variable_price with the amount that the user want to donate.

The code also has a page: /donate and a configuration form /admin/commerce/config/donation_amounts where some predefined amounts can be configured. For more details about the coding part, you can just check out the sandbox project.

While finalizing this blog post I discovered that, since starting to implement this feature on a recent project, a new module called Commerce Donate surfaced on Drupal.org which might be interesting for you too.

Stay in touch – join our newsletter!

 

 

Comments

Matthew Oliveira's picture
Matthew Oliveira

Great post! Thanks for explaining you're approach to donations using Drupal Commerce. Have you ever been asked to support recurring donations?

daniel truninger's picture
Daniel Truninger

@Matthew: We thought that you'll probably enjoy it. Unfortunately we haven't had a case where we had to support recurring donations.

Maciej's picture
Maciej

Also, there is the Commerce Contribution module...

Marwan's picture
Marwan

Nice article!

Does anyone know how to set a rule that allows donations to not go over a certain $ amount?

I need to limit each donation to a maximum of $300 and can't seem to figure out how to do that.

Thanks!

vasi chindris's picture
Vasi Chindris

@Marwan: From what I understand, you want to validate a form using rules. From my point of view, I wouldn't do that with rules, I would check this through a custom module which alters the donation form, adds an additional validation handler and in that validation handler checks the amount that is entered.

If creating a new module is not an option for you, then this module (Rules Forms Support) appears to do the trick. I personally haven't tried it yet, so I cannot give you more details about it.

Robertma's picture
Robertma

Tentez nos applications pour vos envois b2c de SMS. Réalisé pour les PME, notre site vous permet depuis n'importe où de diffuser vos SMS à l'attention de tous vos clients. Capable pour envoyer de très nombreux envois, notre système d'envoi est très rapide pour effectuer vos expeditions en masse.
Doté d'une puissante fonctionnalité de programmation de SMS cliquer ici, notre offre est le summum des logiciels pour l'envoi de SMS et de gestion des campagnes.
Vous élaborez créez facilement vos messages à vers des milliers de propects.
Il vous est possible d'utiliser une API HTTP SMS qui va vous permettre d'envoyer via HTTP ou HTTPS des SMS à l'unité ou en nombre immédiatement ou de façon programmée.
Vous profitez d'une autonomie complète grace à notre interface client. Créez ou chargez votre liste et programmez vos SMS.

Il faut bien définir le but de son message.
Evitez les impérativement envois sans personnalisation.
Couplez l'envoi SMS à un email.
Elaborez méticuleusement le SMS.
Les messages qui arrivent avant leur ouverture sont sur de nombreux téléphones.
Tester sa campagne avec soin.

Add comment

Search form

Latest comments

  • bet at home bonus's picture

    It's great that you are getting ideas from this
    piece of writing as well as from our argument made here.

    bet at home bonus
  • Kira's picture

    Le meilleur soin keratine pas cher

    Kira
  • vps windows's picture

    It'll also save a lot of cash and time for those on a restricted budget who want

    vps windows