An Introduction to Drupal Commerce And A Real World Example

John Robert Wilson
#Drupal | Posted

Drupal Commerce

is a powerful set of modules written by Commerce Guys, most notably Ryan Szrama. It is built on Drupal 7's core functionality as well as some contrib modules. This blog post broadly outlines the concepts of Drupal Commerce. You can find more in-depth information in the official documentation at drupalcommerce.org

The Basics

Drupal 7 introduced the concept of entities to the open source platform. Entities are field-able objects that serve specific purposes but share common base level functionality. You may already be familiar with some existing entities: nodes, users, taxonomy terms & comments.

Drupal Commerce introduces several new entities to your site: Products, Orders, Line Items, Payment Transactions & Customer Profiles.

mockup_d7_commerce_entities

Products

Products represent something you're trying to sell, physical or not. It has a title and a price associated with it. To give an end user the ability to place an instance of a product in their cart, you will need to make a product reference on a node page the end user sees.

Orders

Orders start out as carts. They are a container that holds all of the information given your current shopping experience. It contains line items, which are specific instances of products with options attached. It contains customer information after the order is successful. It also contains any successful and unsuccessful references to payment transactions.

Line Items

Line Items represent an instance of a product in your cart (or order) with options attached.  For example, if a product were a T-Shirt with the ability to choose a color and a user places a T-Shirt in the cart with the red option chosen, a line item with that specific product reference and options is added to the cart.

Payment Transactions

Payment Transactions hold all of the detailed financial metadata for every attempt to checkout with line items in the cart.  Payment transactions usually hold the amount paid, timestamps and some payload information from the payment gateway.

Payment Gateways

There is an undesired cost of resources and labor to store your customers' payment information. That's why most online stores use a payment gateway.  A payment gateway is third party software that you can send transaction information to securely. They process transactions and return information if it succeeds or fails. There are many payment gateways out there like: Paypal, Payflow Pro, Authorize.Net and much much more!

There's even a module for accepting bitcoin as payment!

Checkout Process

mockup_checkout

Here is the checkout process:

  1. A user shops around your site and adds things to his cart.
  2. During the process a user can remove or change the quantity of items.
  3. Once the user is ready to checkout they enter customer profile information, shipping information if applicable, & payment information.
  4. A request is made to the payment gateway for the desired amount.
  5. The payment gateway responds with payment accepted or denied.
  6. If the payment is accepted the user is brought to a confirmation page and a receipt is emailed to them.

From here, the order can be managed and updated by the site administrator or other users with proper permissions.

Commerce Rules

It's important to note that the checkout process has rules integrated into it. Rules can be used to calculate tax on a given order or a discount given some pre-existing conditions. Shipping Rules can be put into place to request shipping quotes from 3rd party web services (Like FedEx or UPS).

Shipping & Inventory

If you do plan on shipping physical products, you'll need to install the Commerce Shipping module so you can leverage customer shipping profiles. This is important to have when a customer's billing profile is different from where they want their purchase to be shipped.

Inventory (or stock) can be managed with another contrib module: Commerce Stock. With this module enabled, you can set the amount of each product and it will decrement the current stock when each successful transaction has gone through.

Tax

Drupal commerce supports two types of tax out of the box.  Sales tax, which is used mostly in the US, and VAT which is more commonly used.  With VAT the total is shown to the customer with the tax already included in the price and nothing is added to the subtotal section.  Tax is implemented through Commerce Rules.

Some Useful Hooks for Developershook_commerce_checkout_router

: Routes checkout/%commerce_order* to an alternate URL if necessary.

hook_commerce_checkout_complete: Allows other modules you make to perform business logic when an order is complete.

hook_commerce_checkout_pane_info: Defines checkout panes available for use on checkout pages.

hook_commerce_checkout_pane_info_alter: Alter an existing checkout pane.

There are tons of them!

Commerce Kickstart

If you want to get up and running right away and play with a nice out of the box installation of Drupal Commerce, check out the Commerce Kickstart installation profile.

A Real World Example

FINAL_RH_logo_RGB

Robin Hood

is a non-profit poverty fighting organization in NYC. In 2011 they invested nearly $146 million to the most effective poverty fighting organizations in NYC. 92% of people who enter Robin Hood funded housing programs don't return to shelters. Robin Hood programs increase the chance of getting a GED by 76%. Robin Hood's job training programs are twice as effective as others, yielding higher salaries and job retention rates than other city-funded programs.

In March, Robin Hood started accepting donations utilizing Drupal Commerce. We used the concept of products to create a customized product type called "donation product."  The donation product displays are referred to by nodes called campaigns. A site administrator can set up a campaign and associate one or many different types of donation products, representing different amounts or the desire to have a recurring donation given a certain frequency of time.

After adding a donation product to their cart, a user is taken to the checkout process where some extra meta-information can be added to your donation, such as being anonymous or making the donation in honor of someone. All of these extra options are stored in fields kept in custom line items called "donation line items."

The donor inputs payment information and upon success is taken to a confirmation page and emailed a receipt for tax purposes.

All of this was made possible by the flexibility of entities and the vast amount of hooks we were able to implement to customize the checkout process.

Go Further!

In conclusion, Drupal Commerce is a nice suite of modules built with lessons learned from the past in mind. I hope this inspires you to open up your own web store or non-profit using Drupal! Here's a list of useful resources that I find helpful and will help you delve deeper into the world of Drupal Commerce!

If you are in the New York area, you can learn more about Drupal Commerce in my NYCCamp session on July 13th!  Hope to see you there!

John Robert Wilson