Drupal Commerce

Deconstructed

All the pieces and how they work together!

Matt Glaman

Developer @ Commerce Guys

Maintainer of Commerce Reports, Commerce Deploy, MojoCart
Co-maintainer of Drupal Commerce, Commerce Kickstart

What makes Drupal Commerce?

Drupal Commerce is about as Drupal-y as they come, harnessing the power of the major and most used modules in the Drupal ecosystem.

Entity API

Drupal Commerce uses the Entity API to the fullest, utilizing five different entities in total.


  • Line items
  • Products
  • Order
  • Customer profiles
  • Payment transactions

It extends core's entity API into a more unified structure, including CRUD operations. Entities in Drupal are a way to handle data and utilize the Field API (simply put.)

Views

No custom code for displaying entities, everything is powered by Views.

  • Administration lists for products, orders, and customer profiles
  • Listing payment transactions on orders
  • Order overview and cart items

Rules

Drupal hooks with a user interface. Setup conditions which react to events and perform specific actions. Drupal Commerce harnesses much of its functionality through Rules rather than standard hook implementations.

  • Product pricing uses Rules
  • Taxes are applied with Rules
  • Payment gateways are Rules actions!

Fields

Last, but not least, Entity integration could not be completed without Field API.

  • Prices handled through Commerce Price field.
  • Reference fields to define relationships
    • Line Item reference fields
    • Product reference fields
    • Customer profile reference fields

Components of Commerce

Drupal Commerce isn't just Drupal-y because it utilizes other contributed projects, but because it is composed of ELEVEN sub-projects itself.


  • Cart
  • Checkout
  • Customer
  • Line item
  • Order
  • Payment
  • Price
  • Product
  • Product pricing
  • Product reference
  • Tax

LOLWUT? SRSLY?

lolwut

Seriously.

But it is a good thing.

Decoupled design allows for complex implementation and ultimate flexibility.

blue Lego wallpaper 1680x1050

PAUSE TIME

Before diving in: there is Commerce Kickstart that provides a demo store and an opinionated installation of a Drupal Commerce site.

 

There are two installation options: demo, or no demo. The demo is great for learning Drupal Commerce and having a best-practices example. However, if you are going to build a Drupal Commerce site, use the no demo install

Price

  • Commerce handles pricing as a field on entities: such as line items, products, and orders
  • Prices support a large array of currency formats.
  • Composed of multiple components which calculate total price
  • Stored in non-decimal format, formatted on display by field formatter

Line items

In my opinion the most integral entity in the Commerce system.

  • Think of line items as a bridge entity for relationships and data
  • Have a quantity attribute
  • There is a unit price field and calculated total price field
  • Relate to an order, expecting to be attached to one.
  • Utilized for shipping services, discounts, coupons, fees, and more.
  • An order is a collection of line items (details soon.)

Product

  • Products must have a unique SKU
  • The product entity represents a variant of an item for sale
  • Example: you have a Medium, Blue, T-Shirt and a Large, Blue, T-Shirt.
  • Fields on the product act as attributes (size, color) with Cart module
  • Intended to be a single product entity for single variation.
  • A purchased item is actually a line item referencing a product entity.

Product reference

  • Product Display: nodes that reference products
  • Provide "add to cart" form (via Cart module)
  • Product reference field defines variations
  • Method to render Product fields on referencing entity

Product pricing

The product pricing module is an API that extends hooks provided by the Price module. It utilizes Rules to adjust the components of a price field to increase or decrease the total price amount

Orders

  • Consider it a "hub" entity
  • Entity which contains line items
  • Order total is calculated through aggregation of computed line item totals
  • Owned by user, identified through uid or mail
  • Have states: cancelled, pending, completed
  • Can be created through the admin UI

Customer profiles

  • Orders have customer profiles attached (like billing)
  • Stores information like address, or phone
  • No way for customer to manage their own profiles, without contrib
  • Required for payment, Commerce Shipping

Payment

  • Way to pay for an order's total
  • Allows for balances, if not paid in full
  • Transactions are entities (not defined as fieldable by default.)
  • Payments can be made through order admin UI.

Checkout

  • User interface for users to "edit" their order
  • Populate customer profile information
  • Create a payment transaction (paid in full)
  • Provides "checkout" state for orders

Carts

  • "Add to Cart" field formatter for product reference fields
  • User interface for customers to create orders
  • Method for a customer to access checkout
  • Turns Product entity fields to purchasable attributes
  • Provides "cart" state to continue pricing rules within orders

Taxes

  • Taxes are price components
  • Work in similar fashion to pricing rules

We made it!

That was a lot. I mean a lot.

Let's do a quick recap

  • Product displays are nodes that have a product reference field.
  • A variant is a product entity that belongs to a product display
  • Product attributes are exposed product fields that users can choose from via "Add to Cart"
  • Think of Checkout as a customer's interface to edit their order
  • Think of Cart as a customer's interface to create an order

Drupal Commerce Distributions

Luckily there are distributions to help you along the way.

Yes, there's more than one!

Commerce Kickstart

Commerce Kickstart provides a demo store installation complete with products, categories, blog, ad placements, and more.

Who should use Commerce Kickstart? Anyone who wants to skip weeks of configuration to build a commerce site on the Drupal Commerce framework.

It's opinionated and a great way to get started on your first project.

Commerce Deploy

Commerce Deploy aims to simplify Drupal Commerce development for site builders and developers. Get started faster and cut the cruft of dependency gathering. The ideology behind Commerce Deploy is to provide enhancements to Drupal Commerce development without getting in the way.

It is a decoupled base distibution composed of several projects - so you can take the whole thing or the parts you want for your own distributions.


  • Core
  • Backend
  • Checkout
  • Customer
  • Product
  • Promotions
  • Shipping

For developers, by a developer.

MojoCart

MojoCart brings together Panopoly and Commerce Deploy to create a robust e-commerce distribution.

Currently in development, MojoCart plans to harness the power of Apps and provide a robust site builder Drupal Commerce experience powered by Panels.

THE END

github/mglaman/deconstructing-drupal-commerce