2. The Order, OrderItem and OrderItemUnit¶
We’re sorry but this documentation section is outdated. Please have that in mind when trying to use it. You can help us making documentation up to date via Sylius Github. Thank you!
Here is a quick reference of what the default models can do for you.
2.1. Order basics¶
Each order has 2 main identifiers, an ID and a human-friendly number.
You can access those by calling
The number is mutable, so you can change it by calling
->setNumber('E001') on the order instance.
<?php $order->getId(); $order->getNumber(); $order->setNumber('E001');
2.2. Order totals¶
All money amounts in Sylius are represented as “cents” - integers.
An order has 3 basic totals, which are all persisted together with the order.
The first total is the items total, it is calculated as the sum of all item totals (including theirs adjustments).
The second total is the adjustments total, you can read more about this in next chapter.
<?php echo $order->getItemsTotal(); // 1900. echo $order->getAdjustmentsTotal(); // -250. $order->calculateTotal(); echo $order->getTotal(); // 1650.
The main order total is a sum of the previously mentioned values.
You can access the order total value using the
It’s not needed to call
calculateTotal() method, as both
adjustmentsTotal are automatically updated after each operation that can influence their values.
2.3. Items management¶
The collection of items (Implementing the
Doctrine\Common\Collections\Collection interface) can be obtained using the
To add or remove items, you can simply use the
<?php // $item1 and $item2 are instances of OrderItemInterface. $order ->addItem($item) ->removeItem($item2) ;
2.4. OrderItem basics¶
An order item model has only the id as identifier, also it has the order to which it belongs, accessible via
The sellable object can be retrieved and set, using the following setter and getter -
In most cases you’ll use the OrderBuilder service to create your orders.
Just like for the order, the total is available via the same method, but the unit price is accessible using the
Each item also can calculate its total, using the quantity (
->getQuantity()) and the unit price.
OrderItemUnit allows better management of
OrderItem’s quantity. Because of that, it’s needed to use OrderItemQuantityModifier to handle
quantity modification properly.
<?php $item = $itemRepository->createNew(); $item->setVariant($book); $item->setUnitPrice(2000); $orderItemQuantityModifier->modify($item, 4); //modifies item's quantity to 4 echo $item->getTotal(); // 8000.
An OrderItem can also hold adjustments.
2.5. Units management¶
Each element from
units collection in
OrderItem represents single, separate unit from order. It’s total is sum of its
item unit price and totals’ of each adjustments. Unit’s can be added
and removed using
removeUnit methods from
OrderItem, but it’s highly recommended to use OrderItemQuantityModifier.