Flexible Drupal Commerce
Five problems I worked through
Drupal Commerce doesn't hand you a fixed store. It hands you entities, fields, Views, and Rules (later, event subscribers), and you build the store the merchant actually has. Over about ten years at Commerce Guys and Acro Media, I spent most of my time helping people use that flexibility on hard problems.
Each problem ends up touching the parts of ecommerce that make projects expensive: taxes, shipping, payment, and fulfillment. Every example links to something public I wrote or a drupal.org issue I'm credited on, so you can check the work. Each one also has a short story to play with. In the stories, blue panels are behind the counter and green panels are what shoppers see.
Taxonomy
A 40,000-product catalog people can actually browse
The problem. In September 2014 a site builder posted on drupalcommerce.org that he had imported close to 40,000 fastener products. Each had a type, a category, and a sub-category: about 40 types, 1,300 categories, and 8,000 sub-categories. He wanted drill-down navigation (Bolts, then Stainless Steel Bolts, then Metric Stainless Steel Bolts) without building a View per category and without Apache Solr yet.
What I recommended. Put a single hierarchical taxonomy on the products and build one View that shows either child terms or products, depending on how far down you are. That left two jobs:
- Tagging 40,000 products. I suggested a Views Bulk Operations view that selects products by matching criteria, plus a small Rules component per term that adds that term to a product. Run the components as bulk operations and the site tags itself.
- One View for the whole tree. I recorded two videos: one for a single hierarchical vocabulary, and one that treats separate vocabularies as levels of the hierarchy.
A month later another store builder replied that the hierarchical Views approach worked for his 150-category catalog.
Why it matters. Taxonomy in Commerce isn't only navigation. The same terms can drive pricing rules, discounts, tax classes, and product feeds. My later Autotrader CSV module, for example, creates the vocabulary Autotrader's feed requires on install and maps to Autotrader's category IDs.
Related: an earlier Q&A answer on building a catalog in Kickstart 2 (Aug 2013), where I offered the menu system as a simpler alternative to taxonomy plus Search API.
This story needs JavaScript.
Pricing
When the price on the order isn't the price on the product
Pricing is where merchants' real business rules show up: tiered quantity discounts, role-based prices, orders keyed in by staff, and prices that need to hold still while a customer checks out. I've worked on this from both the documentation side and the code side.
Order admin and cart recalculation (Commerce 1.x). In December 2014 I wrote up three order management features for store administrators:
- Apply pricing rules re-runs product pricing rules on an existing order. It resets product prices and throws away custom prices typed in on the edit form, so it has to be used on purpose.
- Simulate checkout completion fires the checkout completion events for orders built in the back office, which may change order status and send email.
- Shopping cart refresh settings control how often cart prices are recalculated, separately for the order's owner and for administrators. Recalculating on every page load is flexible but slow; the default holds prices for 15 seconds.
Quantity pricing (Commerce 2.x). I maintain Commerce Quantity Pricing. The key design fix was moving pricing into order item adjustments instead of a presave hook, so discounts show up as line-level adjustments that tax and reporting can see. I also added a fallback when no tier is set.
Other pricing credits. In Commerce core I'm credited on a fixed-amount-off offer for orders (2017), using the calculator for quantity math (2016), and a fix where an adjustment amount was wrongly required on every order. I also maintain Commerce Order Sub-total, where I added Commerce Discount support.
Where tax comes in. Every one of these touches tax. A discount applied before tax gives a different total than one applied after. On a QuickBooks Online integration I'm credited on re-pulling tax rates from QuickBooks so the store and the accounting system agreed.
This story needs JavaScript.
Currency
Selling across borders without rewriting the catalog
Commerce Guys hired me in 2012 to revamp the site builder documentation on drupalcommerce.org. The user guide's Currency Conversion and Shopping Cart and Multi-Currency pages come from that effort.
How it works in Commerce 1.x. Core handles a narrow but useful case. You enable extra currencies, then write a product pricing rule that:
- Checks the line item's unit price currency (so only, say, USD prices pass through).
- Multiplies the amount by a fixed exchange rate (the guide used 0.76 EUR per USD).
- Converts the unit price to the new currency code.
The product data never changes; the cart and product page show converted prices. Core also handles mixed-currency orders by converting line items to the site's default currency, or to the first line item's currency if the default isn't on the order. For live exchange rates, the guide points to Commerce Multicurrency.
The guide says plainly that the static-rate exercise has a limited use case. The point was to teach pricing rules, since every serious store ends up living in them.
Commerce 2.x. Currency moved into the store and price field configuration. In Commerce Quantity Pricing I'm credited on using the site's available currencies instead of only USD and CAD, which mattered for stores outside North America.
Where tax and payment come in. Currency usually travels with a billing location. That changes which tax applies and which payment gateway can settle the money, which is why multi-currency work rarely stays a pricing-only problem.
This story needs JavaScript.
Multiple carts
The “bug” that's really a marketplace feature
This is the best example of a small question turning into taxes, payment, and fulfillment. In May 2020 I wrote Multiple ecommerce shopping carts in Drupal Commerce for Acro Media because many people evaluating Commerce 2.x want to either get rid of the extra carts or check out with all of them at once.
Why there are two carts. The post walks through reproducing it: add something to the cart as a logged-in user, log out, add something as anonymous, then log back in. You now have two carts. Commerce 2.x shows them by design, because the system won't delete a customer's cart. Under the hood, Commerce 2.x keeps one active cart per order type and store (Bojan Zivanovic's 2015 post explains the store model).
Trade-offs I laid out.
| Keeping multiple carts helps | Keeping multiple carts costs |
|---|---|
| Customers never lose a cart | Customers may have to check out more than once |
| A marketplace with many sellers can build on it | Payment and fulfillment have to be handled per cart or per vendor |
| Different checkout flows for digital, recurring, and shipped items | The cart dropdown and cart page have to show more than one cart and checkout button |
If you want one cart. Two contrib modules solve it: Commerce Combine Carts and Commerce Cart Advanced (maintained by my Acro colleague Dimitris Bozelos).
If you want one checkout across many stores. This is the Etsy or Amazon model. Bojan's advice was a custom form that acts like a checkout flow but changes several orders at once. The code isn't the only problem. I listed the business questions that decide whether it's worth doing:
- Fulfillment: each store should only see its own items.
- Order management: split orders change totals and confuse customers and support staff.
- Payment: either one site-wide gateway plus payouts to sellers, or separate gateway credentials per store.
- Taxes: tax rules would need to be set up and maintained per store, and services like Avalara AvaTax work per store and can cost too much for small sellers.
Related code. I maintain Commerce Wishlist, which
reuses the add-to-cart pattern. I built the scaffolding that mirrors add-to-cart
and the add-to-wishlist button in 2016. In Commerce core I'm
credited on the Shopping Cart block and on
clarifying emptyCart() vs. removeOrderItem()
in the CartManager.
This story needs JavaScript.
Organic Groups
Selling access, not just products
The problem. A site builder named Mike asked how buying a product could add the customer to an Organic Group, the pattern behind paid courses, member communities, and gated content. It came from the drupalcommerce.org Q&A, and in November 2013 I answered it with a video and a post, Selling Organic Groups using Drupal Commerce.
The approach. No custom module, just site building:
- Add a product reference field to the Organic Group content type, and have each group reference its product.
- Trigger a rule on “After an order is first paid in full.”
- Loop over the order's line items.
- For each one, a Rules component finds the product display that references the line item's product.
- A second component checks whether that display is an Organic Group, and if so, adds the customer as a member.
I published the exported rules so people could import them rather than click them together.
What the community added. A commenter pointed out an edge case: with anonymous checkout, the user account is created at checkout completion, after the “paid in full” event fires, so there's no user to add yet. His fix was to trigger on checkout completion, check that the balance is zero, and order the rules so account creation runs first. He also covered bundles, like selling group 1 and group 2 together for less than separately. That's the kind of detail that decides whether a membership store works in production.
Related work. I'm credited on a fix to Role Expire's Rules action, another way to sell time-limited access, and my drupal.org profile lists me on the documentation guide for Subgroups for Organic Groups.
This story needs JavaScript.
The code behind it
I have 288 issue credits on drupal.org and maintain about 30 projects, most of them in the Commerce ecosystem. The table pulls out the ones that deal with shipping, tax, pricing, and order handling, the areas where the five problems above end up.
Shipping is the thread I haven't written a post about yet. Stacking Dimensions, which adjusts an order's volume for products that stack, would make a good next write-up.
What's real here
The write-up, its links and the credits are real. The stories are small JavaScript sketches of the behavior each section describes, not Drupal. The fasteners, prices, stores, people and exchange rates are made up. Each story's “How Drupal does this” note, and any Rules event or contrib module behavior it names, follows the linked post or the module's code.
Sources
My writing
- Selling Organic Groups using Drupal Commerce, drupalcommerce.org, Nov 15, 2013
- Commerce 1.x enhances cart calculation and order management, drupalcommerce.org, Dec 3, 2014
- Navigating Drupal Commerce Products (my reply of Sep 5, 2014), drupalcommerce.org
- How do I build a product catalog using Kickstart 2? (my answer of Aug 7, 2013), drupalcommerce.org
- Multiple ecommerce shopping carts in Drupal Commerce, Acro Commerce, May 12, 2020
Documentation and context
- DrupalCommerce.org Redesign in Progress, drupalcommerce.org (my hiring to revamp the user guide)
- Currency Conversion, drupalcommerce.org user guide
- Shopping Cart and Multi-Currency, drupalcommerce.org user guide
- Commerce 2.x Stories: Stores, Bojan Zivanovic, drupalcommerce.org, Nov 10, 2015
Credits and projects
- joshmiller on drupal.org, profile and maintained projects
- Contribution records credited to joshmiller, 288 issue credits