Building a small shop on Statamic with Simple Commerce
On this page
In March I added a shop to my drone site. The products are 3D-printed parts, a few dollars each, in a choice of colors, shipped by UPS. The site was already Statamic, so the shop is Simple Commerce: the release current in March when I built it, moved to the next majors of both in April. I kept Duncan McClean's starter kit checked out next to the project as the reference for what the sc: tags are called and what they expect, and built the templates in my own site's markup.
Collections and the product blueprint
Products, orders and customers are ordinary collections. The config points each at an entry repository, and the orders collection ends up as markdown files like everything else. The products collection has its own route, /shop/products/{slug}.
The product blueprint is where the shop-specific decisions live: a price field of type money (an entry stores price: 999 for $9.99), a color_upcharge float, an available_colors taggable with the colors I print in, a lead_time text field that shows on the product page, a stock integer, and a package_dimensions field from my UPS addon so the shipping rate has something to work with. Black is always the default color at no extra cost; anything else adds the upcharge.
Routes and the three-step checkout
The pages are plain Route::statamic() entries in routes/web.php, one per template:
Route::statamic('shop', 'products.index');
Route::statamic('shop/cart', 'products.cart');
Route::statamic('shop/checkout', 'products.checkout.information');
Route::statamic('shop/checkout/shipping', 'products.checkout.shipping')
->middleware(ValidateShippingAvailability::class);
Route::statamic('shop/checkout/payment', 'products.checkout.payment');
Route::statamic('shop/success', 'products.success');
The product page wraps its form in {{ sc:cart:addItem }} with a hidden product input, a quantity capped at stock, and a hidden color input driven by Alpine buttons. color has to be listed under line_items in the config's field_whitelist or the addon drops it. The cart page is {{ sc:cart }} with {{ sc:cart:updateItem :item="id" }} around a quantity input that submits on change, and {{ sc:cart:removeItem :item="id" }} around a button.
Checkout is three pages. Information is {{ sc:cart:update redirect="/shop/checkout/shipping" }} with name, email and the shipping address, plus a toggle for a separate billing address. Shipping is another sc:cart:update whose only input is a radio list from {{ sc:shipping:methods }}, showing {{ name }} and {{ cost }}. Payment is {{ sc:checkout }} with the gateway partial inside it. Each page starts with a redirect to the cart if {{ sc:cart:count }} is zero.
Stripe's Payment Element
The gateway config is the built-in StripeGateway with a key and secret. The partial reads three values from the gateway tags into hidden inputs, {{ stripe:config:key }}, {{ stripe:client_secret }} and {{ stripe:callback_url }}, and mounts a Payment Element in the site's colors. The form's submit handler disables the button and dispatches a StripeConfirmPayment event; the Stripe script listens for it and calls stripe.confirmPayment() with the callback URL as return_url. If Stripe returns an error it dispatches StripePaymentFailed and the button comes back. Stripe redirects to the callback, Simple Commerce marks the order paid, and the customer lands on the success page.
Where I had to step outside the addon
The color upcharge. Price is per product, and a color is line item metadata, so the extra cost goes through SimpleCommerce::lineItemPriceHook() in the service provider: read color from the line item's metadata, and if it is not Black add color_upcharge * 100 to the product price.
Shipping that cannot be rated. The UPS method returns unavailable when the address cannot be rated, and the shipping page would simply show an empty radio list. The ValidateShippingAvailability middleware on that route loops the configured methods, calls checkAvailability() on each, and if none passes sends the customer back to the information page with ?shipping_error=1, where the template explains that the state and postal code probably need another look.
The order after a Payment Element callback. What I found is that with the Payment Element flow the gateway callback writes the order directly, so the order title stayed blank, the status_log never got its "placed" entry and the customer's orders list was not updated. An EntrySaved listener on the orders collection backfills all three and saves quietly. I would rather not be in that code, but it is one listener with a comment explaining why.
Clearing the cart. The success page sends a DELETE to /!/simple-commerce/cart from a two-line script, so a refresh does not show the old order.
Email. The order_paid notifications ship with the addon but arrive commented out. I turned on CustomerOrderPaid to the customer and BackOfficeOrderPaid to an address from .env, and moved the site's mail driver to Resend the same day so they would actually go out.
The deploy. The UPS addon lived at ../simple-commerce-ups on my machine and composer.json pointed at it as a path repository. That works until the site deploys on a server where the sibling folder does not exist: Forge failed with "Source path is not found". The fix was a VCS repository entry pointing at the public GitHub repo, pinned to a branch until the addon's release for the new major was tagged. If a package only exists on your laptop, the deploy is the first place you will find out.
The short version
Simple Commerce gives you the collections, the cart tags and the gateway; you give it templates, a product blueprint that matches what you sell, and a handful of hooks for the things that are yours alone (a surcharge, a rate that cannot be calculated, an email that needs to go out). Read the starter kit for the tag names and build the pages in your own markup. And never point production at a path on your desk.