Ruling with Drupal Commerce
Drupal commerce is pretty sweet out of the box, but there are a few ways we can boost the experience and management with one simple module - Rules. It’s one of the dependencies for commerce, but you may not notice it in action, unless you need it. Today we’ll look at a few ways you can utilize rules to improve the commerce experience.
First let’s check out donations. Drupal Commerce Donate has been around for a few years, although it’s still in beta. It centers on the premise to provide the user a way to donate a set amount from a list, or to select other and choose their own amount. We can do this easily utilizing Rules and the Select or Other module. Once the Select or Other module is in your sites/all/modules folder and enabled, we’ll go to admin/commerce/config/line-items to create a new line item type. I called mine “Donation”.
Next add a field with type set to Integer, and for Widget one of the Select or Others. Under “Available Options” you can type in a few options for the users to select, and I like to choose common donation amount ($10, $25, $50 and $100). Donors have the “other” option as well. Next scroll down to the “Add to Cart Form Setting” and check the box. We want this value included on the add the cart form.
Now we need to either create a new content type or add a field to an existing content type for the product reference. Once the field is created we need to update the display for the fields “add to cart”, setting it to the newly created line item.
With the background complete, go create a product type, I called this Donation as well, then create a new product. You can set the price of that product to whatever you want, as the rule will update it on the fly with the user selection. I called my product Donate Today! Last we need to create a new piece of content and add the new product to it.
Now we’ll create the donation rule. The rule has 3 basic parts:
- An Event - This is what needs to happen to trigger the action below.
- A Condition - These are any conditions that need to be fulfilled prior to the action firing.
- An Action - This is what we want to happen.
The event in this case will be “Calculating the sell price of a product”. We do want to add the condition “Entity has field”, since we only want the sell price updated on products that contain the field line item we created. The “Data selector” should be “commerce-line-item” and use the value dropdown to select the field.
We’ll now have 2 actions, the first being “Calculate a value”. The data selector should be “commerce-line-item:field_donation” (or whatever your field is called). The operator is the star, and the second input is 100. We need to do this simple math to have the value format properly. Note the variables for the Calculation result at the bottom, we’ll use that in the next action. Last action is “Set the unit price to a specific amount”. Data selector for line item is “commerce-line-item”, and the amount data selector should be set to result. We want to update the Base price (price component type) and not round at all (price rounding mode). Save and that’s it.
Please note this does not work with Rules version 2.8. Rules is currently on 2.9, so make sure you update to save yourself a HUGE headache! That may or may not be from personal experience…
Now let's work on Updating the Order Status.
You’ll notice when an order is placed and completed with Drupal Commerce, it will stay in the state of pending. This is deliberate, and wonderful, as it leaves the site open to complete customization. You can tweak add rules and a workflow as needed. This next rule is very basic and is based off a paypal system.
I’m calling this rule “Setting the order status on checkout completion” (I like to be very detailed in naming of the rule). The event is “Completing the checkout process”, which is self explanatory so I don’t need a condition. If you only wanted this to apply to a specific content type you can add that condition. The action is “Update the order state”, with the Order to Update data selector set to “commerce-order”, and the Order State set to “Completed”.
Save, and now any orders (or donations) will be updated to complete upon completing the checkout process. Other actions can be easily added to the above as well, such as sending out a detailed email to a select person or persons or showing the user a specific message on the site. This can be especially useful if the order is a down payment or reservation.