Skip to content
Darin Larimore
All posts

UPS shipping rates for Statamic's Simple Commerce

5 min read
On this page
  1. Three pieces
  2. The cache took three tries
  3. Eighteen months on a shelf
  4. The short version

Simple Commerce gives you a ShippingMethod contract with two methods that matter, calculateCost() and checkAvailability(), and leaves the number up to you. A flat rate is fine until someone orders something heavy or far away, and then you want the carrier to tell you what it actually costs. At the end of September 2024, on the Statamic and Simple Commerce releases current at the time, I wrote simple-commerce-ups to do that: pack the cart into real UPS boxes and ask the UPS Rating API for the price.

Three pieces

Packing. UPS rates a shipment by package dimensions and weight, not by "a cart". So every product gets a package_dimensions fieldtype (length, width, height, weight, and a "package separately" toggle), and the addon hands each line item to dvdoug/boxpacker along with a list of boxes. The box list ships with the standard UPS sizes (Letter, Tube, 10KG, 25KG and the three Express boxes) in content/boxes.yaml, plus a small control panel page under Simple Commerce for adding your own. Everything is converted to millimeters and grams on the way into the packer, because it wants integers, and back to inches and pounds (or cm and kg) on the way to UPS, because it wants whatever unit you declared.

Digital products and products with no dimensions are skipped. If nothing is left to pack there is no rate, and the method reports itself unavailable.

The request. The Rating API wants an OAuth client-credentials token first, so getClient() posts the client id and secret to security/v1/oauth/token and builds a Guzzle client with the bearer. Then one POST api/rating/v1/Shop with the ship-from address from config, the ship-to address from the order, and one Package entry per packed box. With an account number in config it also asks for negotiated rates. Shop returns every service UPS will sell you for that shipment, so the addon keeps the service code table (03 is Ground, 02 is 2nd Day Air, and so on) and picks out the one the shipping method asked for. Monetary value times 100, because Simple Commerce works in cents.

The glue. php please make:ups-shipping-method UPSGround writes app/ShippingMethods/UPSGround.php from a stub, and its calculateCost() is one line:

public function calculateCost(Order $order): int
{
    $ups = new UPS();

    return $ups->fetchShippingRates($order, 'UPS Ground');
}

Add the class to the shipping methods array in config/simple-commerce.php and {{ sc:shipping:methods }} starts showing a real price.

The cache took three tries

Simple Commerce calls checkAvailability() and calculateCost() separately, for every configured method, while rendering one checkout page. Without a cache that is two UPS calls per method per page load, each with a token request in front of it. The first day's commit history is me getting this wrong in the obvious ways.

The first version cached the response in a named Blink store and raced with itself; I ripped it out the same night with the commit message "remove cache from response because of race condition" and cached the request payload instead, which saved nothing because the HTTP call was still outside the cache. Two days later it cached the parsed RatedShipment list under a key built from the order id, a hash of the shipping address and a hash of the line items. The key changed once more, from hashing the whole line item collection to hashing just the product ids. The whole thing lives in Blink, so it only ever spans one request. That is the right lifetime: the goal was to stop asking UPS twice in one page load, not to remember rates between visits.

Eighteen months on a shelf

In March 2025 I added the control panel box page, the metric option and the package-separately toggle, and wrote the README with screenshots of the UPS developer portal, because getting the credentials is the part people get stuck on. You create an app with the Rating and OAuth products enabled, and the client id, secret and account number go in .env.

Then in March 2026 I put it on my own shop, a small store for 3D-printed drone parts, and the first day of wiring it in, running carts through with real addresses, found four bugs:

  • A network-level Guzzle exception has no response, so the error handler that read $e->getResponse()->getBody() was a fatal error sitting on top of the real error. It now checks for a response before reading one.
  • StateProvinceCode was always sent. Outside the US and Canada there usually is no region, and UPS rejects an empty one. It is now filtered out of the payload when the address has none.
  • fetchShippingRates($order, 'UPS Ground') returns nothing for an address in another country, because Ground is domestic. Shop was already returning Worldwide Expedited and friends; the addon just was not looking at them. I added fetchCheapestRate(), which sorts whatever services came back and returns the lowest, and my shop's shipping method uses that instead of a named service.
  • The boxes page in the control panel was registered with the statamic.cp middleware but not statamic.cp.authenticated, so a logged-out visit crashed on a null user in the layout instead of redirecting to login.

None of those paths exist in a test cart shipped from my own zip code to my own zip code. An addon that has only run against my account is not finished, it is just not failing yet.

The short version

Carrier rating is packing plus one HTTP call, and the packing is the part that needs a library. Cache per request, not longer. And the bugs that matter live in the paths your test cart never takes: the address without a region, the exception without a response, the country the service does not serve.