Shipments¶

A Shipment is a representation of a shipping request for an Order. Sylius can attach multiple shipments to each single Order. Shipment consists of ShipmentUnits, which are a representation of OrderItemUnits from its Order.

How is a Shipment created for an Order?¶

Warning

Read more about creating Orders where the process of assigning Shipments is clarified.

Splitting shipments¶

As mentioned in the beginning Sylius Order holds a collection of Shipments. In Sylius Plus edition Orders can be fulfilled partially, therefore it is possible to split the default Order’s shipment.

To do it Sylius Plus provides a UI, where you can choose which items from the initial shipments you’d like to extract to a new split shipment and send it (providing a tracking code or not). Shipments of an Order can be split as long as there remains one shipment in state ready.

../../_images/banner5.png

The Shipment State Machine¶

A Shipment that is attached to an Order will have its own state machine with the following states available: cart, ready, cancelled, shipped.

The allowed transitions between these states are:

transitions:
     create:
         from: [cart]
         to: ready
     ship:
         from: [ready]
         to: shipped
     cancel:
         from: [ready]
         to: cancelled
../../_images/sylius_shipment.png

Shipping Methods¶

ShippingMethod in Sylius is an entity that represents the way an order can be shipped to a customer.

How to create a ShippingMethod programmatically?¶

As usual use a factory to create a new ShippingMethod. Give it a code, set a desired shipping calculator and set a zone. It also need a configuration, for instance of the amount (cost). At the end add it to the system using a repository.

$shippingMethod = $this->container->get('sylius.factory.shipping_method')->createNew();

$shippingMethod->setCode('DHL');
$shippingMethod->setCalculator(DefaultCalculators::FLAT_RATE);
$shippingMethod->setConfiguration(['channel_code' => ['amount' => 50]]);

$zone = $this->container->get('sylius.repository.zone')->findOneByCode('US');
$shippingMethod->setZone($zone);

$this->container->get('sylius.repository.shipping_method')->add($shippingMethod);

In order to have your shipping method available in checkout add it to a desired channel.

$channel = $this->container->get('sylius.repository.channel')->findOneByCode('channel_code');
$channel->addShippingMethod($shippingMethod);

Shipping Zones¶

Sylius has an approach of Zones used also for shipping. As in each e-commerce you may be willing to ship only to certain countries for example. Therefore while configuring your ShippingMethods pay special attention to the zones you are assigning to them. You have to prepare methods for each zone, because the available methods are retrieved for the zone the customer has basing on his address.

Shipping Cost Calculators¶

The shipping cost calculators are services that are used to calculate the cost for a given shipment.

The CalculatorInterface has a method calculate() that takes object with a configuration and returns integer that is the cost of shipping for that subject. It also has a getType() method that works just like in the forms.

To select a proper service we have a one that decides for us - the DelegatingCalculator. Basing on the ShippingMethod assigned on the Shipment it will get its calculator type and configuration and calculate the cost properly.

$shippingCalculator = $this->container->get('sylius.shipping_calculator');

$cost = $shippingCalculator->calculate($shipment);

Built-in Calculators¶

The already defined calculators in Sylius are described as constants in the SyliusComponentShippingCalculatorDefaultCalculators

  • FlatRateCalculator - just returns the amount from the ShippingMethod’s configuration.
  • PerUnitRateCalculator - returns the amount from the ShippingMethod’s configuration multiplied by the units count.

Shipment complete events¶

There are two events that are triggered on the shipment ship action:

Event id
sylius.shipment.pre_ship
sylius.shipment.post_ship