Guide 08 · Developer docs

Commerce

A Core dna store is a set of records and an API that moves a purchase from cart to order: products described in every language and priced in every currency the store sells in, a cart, a checkout that becomes an order, invoices, payments, tax and shipping. The site builds the storefront on top: the cart and checkout pages, their scripts, and the customer emails.

L2–L3
Unit 06 · Tax and shipping configuration

Shipping methods and their templates

Every shipping method is a template that creates the rates a shopper chooses from. The parts of shipping, what a method's template receives, a complete two-country set-up with weight-banded prices in both template languages, currencies and languages, goods with nothing to ship, and what changes with inventory tracking on.

Who this is for. Developers setting up a store's shipping and writing its rate logic. After this page you can configure shipping for the places a store serves and price it by weight, destination and currency, in the site's template language, without framework code.

The parts of shipping

PartWhat it is
Shipping categoryWhat a product is for shipping: parcel, oversize, chilled. A product has one; a variant uses its product's. Products without one use the Default shipping category (Shipping.defaultShippingCategory, required). Its Separate fulfillment switch gives the category's products a fulfilment of their own while inventory tracking is on.
GroupEvery method belongs to a group, which has a name, an identifier ("Unique identifier to define a group even if it has the same name with any other group"), a Type (Delivery, Pickup, or empty for Delivery and pickup) and the Same warehouse switch, on by default. The Type and Same warehouse are read while inventory tracking is on.
Shipping methodOf type Template ("Specify a custom template to create rates manually"): a Name, the Active switch, its group, the shipping categories it carries, the warehouses it serves, its Template code, and, under Fulfillment, the Nothing to ship switch.
RateOne option the shopper chooses from: a name, a total, an optional service level and courier name, and an expiry, after the Lifetime (Shipping.rates.lifetime, 3,600 seconds).

The categories, the group's Type and the warehouses decide which fulfilments a method may carry; the template decides where it ships and what it charges. With inventory tracking off, a method's warehouses are read only for a pickup, and the group's Type is not read (With inventory tracking on).

What a method's template receives

A method's Template code runs for each fulfilment when shipping is created on a checkout, in the site's template language, Smarty or Twig. What it prints is not shown anywhere: rates exist because the code calls create on the rates helper, once for each option it offers. The code receives:

VariableWhat it is
fulfillmentThe items being shipped. getFulfillmentItems() gives each item, with getItem() (the line, whose getProduct() is the product or variant added) and getQuantity(); getDeliveryAddress() is the delivery address, empty for a pickup; isPickup() is true when the shopper collects the items; getWarehouse() is the warehouse they leave from: the pickup warehouse and, with inventory tracking on, a delivery's warehouse too; getOrder() is the checkout.
ratesThe rates helper, with one method: create(total, name, serviceLevel, courierName); the last two are optional.
site, userThe current site and user.
siteDataThe site-wide key-value store: get() and set().
queryThe query helper, as $_query is in page templates: findBy(), findOneBy() and the rest.

The admin's own examples are $rates->create('100', 'Standard') and $rates->create('200', 'Express', 'exp', 'Bob Smith') in Smarty, and rates.create('100', 'Standard') in Twig. For each rate:

  • total is a string holding a number of zero or more, in the checkout's currency, entered the same way as product prices: with tax or without, as Prices that include tax sets. A value that is not a number, or is negative, creates no rate.
  • name is what the shopper sees. serviceLevel and courierName are stored on the rate and returned with it.
  • Code that stops with an error creates no rates for that fulfilment, so guard every value that can be empty, above all the delivery address, which a pickup does not have.

Weight is each product's and variant's getWeight(): a decimal string, or null when no weight was entered. It carries no unit, so the store picks one (kilograms below) and enters every weight in it.

A complete two-country set-up

An Australian store ships parcels in Australia and to New Zealand, prices in Australian dollars, and keeps weights in kilograms. Its shipping takes five steps:

  1. Create a shipping category Parcel and make it the Default shipping category, so every product without a category of its own is carried too.
  2. Create a group Delivery, identifier delivery, Type Delivery.
  3. Enter a weight in kilograms on every product and variant.
  4. Create a shipping method of type Template: name Post, group Delivery, category Parcel, Active on.
  5. Give it the template below. It reads the delivery country and creates rates only for the two countries the store serves, priced by the parcel's weight.
Template code Shipping method "Post": Template code
{$address = $fulfillment->getDeliveryAddress()}
{$country = null}
{if $address}{$country = $address->getCountry()}{/if}
{$code = ''}
{if $country}{$code = $country->getAlpha2Code()}{/if}

{$weight = 0}
{foreach $fulfillment->getFulfillmentItems() as $fulfillmentItem}
  {$product = $fulfillmentItem->getItem()->getProduct()}
  {if $product && !is_null($product->getWeight())}
    {$weight = $weight + $product->getWeight() * $fulfillmentItem->getQuantity()}
  {/if}
{/foreach}

{if $code == 'AU'}
  {if $weight <= 1}
    {$rates->create('9.95', 'Standard post', 'standard')}
  {elseif $weight <= 5}
    {$rates->create('14.95', 'Standard post', 'standard')}
  {elseif $weight <= 20}
    {$rates->create('24.95', 'Standard post', 'standard')}
  {/if}
  {if $weight <= 5}
    {$rates->create('19.95', 'Express post', 'express')}
  {/if}
{elseif $code == 'NZ'}
  {if $weight <= 2}
    {$rates->create('25.00', 'International post', 'international')}
  {elseif $weight <= 10}
    {$rates->create('45.00', 'International post', 'international')}
  {/if}
{/if}
{% set address = fulfillment.getDeliveryAddress() %}
{% set country = address ? address.getCountry() : null %}
{% set code = country ? country.getAlpha2Code() : '' %}

{% set weight = 0 %}
{% for fulfillmentItem in fulfillment.getFulfillmentItems() %}
  {% set product = fulfillmentItem.getItem().getProduct() %}
  {% if product and product.getWeight() is not null %}
    {% set weight = weight + product.getWeight() * fulfillmentItem.getQuantity() %}
  {% endif %}
{% endfor %}

{% if code == 'AU' %}
  {% if weight <= 1 %}
    {{ rates.create('9.95', 'Standard post', 'standard') }}
  {% elseif weight <= 5 %}
    {{ rates.create('14.95', 'Standard post', 'standard') }}
  {% elseif weight <= 20 %}
    {{ rates.create('24.95', 'Standard post', 'standard') }}
  {% endif %}
  {% if weight <= 5 %}
    {{ rates.create('19.95', 'Express post', 'express') }}
  {% endif %}
{% elseif code == 'NZ' %}
  {% if weight <= 2 %}
    {{ rates.create('25.00', 'International post', 'international') }}
  {% elseif weight <= 10 %}
    {{ rates.create('45.00', 'International post', 'international') }}
  {% endif %}
{% endif %}

The country is read from the address's country record, which a checkout address has when its countryName matches the country list (Writing the email and addresses), and compared by its two-letter code. An address with no country record, or in another country, gets no rate from this method.

With this configuration, creating shipping on a checkout gives these rates:

Delivery addressParcel weightRates offered
Melbourne, Australia0.4 kgStandard post 9.95, Express post 19.95
Melbourne, Australia3 kgStandard post 14.95, Express post 19.95
Melbourne, Australia12 kgStandard post 24.95
Auckland, New Zealand1.5 kgInternational post 25.00
Auckland, New Zealand12 kgNone
London, United KingdomanyNone

Every total is in the checkout's currency, here AUD. Where no rate is offered, the checkout's page tells the shopper why (When no method covers the address). If a method that should offer a rate does not, work through Making a shipping method appear at checkout.

Currencies and languages

A rate is created in the checkout's currency, so on a multi-currency store each total has to be the price in that currency. Read the checkout's currency code from the fulfilment and keep one price per currency:

Template code
{$standard = ['AUD' => '9.95', 'NZD' => '10.95']}
{$currencyCode = $fulfillment->getOrder()->getCurrencyCode()}
{if isset($standard[$currencyCode])}
  {$rates->create($standard[$currencyCode], 'Standard post', 'standard')}
{/if}
{% set standard = {'AUD': '9.95', 'NZD': '10.95'} %}
{% set currencyCode = fulfillment.getOrder().getCurrencyCode() %}
{% if standard[currencyCode] is defined %}
  {{ rates.create(standard[currencyCode], 'Standard post', 'standard') }}
{% endif %}

A currency with no price in the list gets no rate, which keeps a method from charging an amount meant for another currency. How a cart's currency is chosen is on Prices and currencies.

A rate's name is stored as the template wrote it. On a multilingual store, give every rate a serviceLevel (standard, express, international) and let the checkout page print the label for that service level in the page's language, falling back to the rate's name.

One method or several

A method's categories and warehouses apply to every rate it creates, so add a method wherever those differ. A store with bulky goods adds a category Oversize on those products and a second method, "Courier", that carries Parcel and Oversize; a fulfilment holding an oversize item then gets its rates from Courier alone. A store that offers collection adds a method in a group whose Type is Pickup, with its pickup warehouses, whose template creates a free rate for a pickup: {if $fulfillment->isPickup()}{$rates->create('0', 'Collect in store', 'pickup')}{/if} in Smarty, {% if fulfillment.isPickup() %}{{ rates.create('0', 'Collect in store', 'pickup') }}{% endif %} in Twig. With inventory tracking on, a delivery leaves from a warehouse too, so the fulfilment's type, not its warehouse, tells a collection apart.

Goods with nothing to ship

Downloads, gift cards and services are not shipped, but a checkout still needs a selected rate on every fulfilment before it can be paid. Give them a shipping category of their own, such as Digital, and a method that carries it, with Nothing to ship switched on under Fulfillment and a template that creates one free rate: {$rates->create('0', 'Digital delivery', 'digital')} in Smarty, {{ rates.create('0', 'Digital delivery', 'digital') }} in Twig. With inventory tracking on or off, a method with Nothing to ship:

  • has its rate selected for the shopper when it is the only rate for a fulfilment, so those items need no shipping choice;
  • has the fulfilment marked delivered as soon as the order is paid, with the status Delivered digitally, and no packing slip or shipments;
  • rates deliveries and pickups alike, whatever its group's Type.

With inventory tracking on, a product whose shipping methods all have Nothing to ship holds no stock, and its items get a fulfilment of their own, without a warehouse. Keep the category off the methods that send parcels: a category that one of them also carries holds stock, because the shopper may choose that method. With tracking off, a checkout keeps all its items in one fulfilment, so a cart that mixes these goods with posted ones is rated by the methods that carry both categories.

Switching Nothing to ship on for a method that already has open fulfilments offers to complete them as well. Saving with that box ticked marks them delivered in the background, and you get a notification when it is done.

With inventory tracking on

When Enable inventory tracking is on, under Transactions › Configuration › Inventory, every order is allocated to warehouses before shipping is rated, and a checkout can have several fulfilments, each with its own rates. Four settings then decide more than they did:

  • A method's warehouses are where it ships from, for a delivery too. An item leaves only from a warehouse that an active method ships from, where that method carries the item's category and serves that kind of fulfilment. A category whose only method lists one warehouse is sent from that warehouse. A method with no warehouses listed ships from any warehouse.
  • The group's Type. A Delivery group's methods rate only deliveries and a Pickup group's only pickups; a group with no Type rates both. Methods with Nothing to ship rate both.
  • Same warehouse. On, a click and collect order is collected from one warehouse. Off, a shopper whose warehouse is short of an item may collect the rest from one other warehouse, and only methods of groups with it off are offered for that order. It is on for every group until you switch it off.
  • Separate fulfillment. On a shipping category, it gives the category's products a fulfilment of their own, even when they leave from the same warehouse as the rest of the order. Each fulfilment is a separate parcel and is charged its own shipping.

How the warehouse is chosen is set under Stock allocation on the same page (Inventory settings). When an item cannot be placed, creating shipping answers 422 instead of offering rates (Checkout says some items cannot be fulfilled).

The rates at checkout

Creating shipping on a checkout runs every active method that may carry the fulfilment and gathers the rates they create into one list, which the shopper chooses from (Creating shipping and showing the rates). A rate lasts for the Lifetime, an hour by default, and a checkout whose selected rate has expired creates shipping again (A rate lasts an hour). Tax on the shipping is the tax provider's, from its Shipping Tax Code (Tax categories and provider codes).

Go deeper

Was this page helpful?

Your answer goes to the docs team.