Skip to main content

Overview

The Order model is one of the key models in Spree. It provides a central place around which to collect information about a customer order - including line items, adjustments, payments, addresses, return authorizations, and shipments.

Order Attributes

Order methods

Here is a list of methods you may find useful: To see all available methods, please check the Order model source code.

The Order State Machine

Each Order flows through a state machine, beginning at a cart state and ending up at a complete state.
This is the default order flow and can be adjusted or overhauled completely.More on this topic at Customizing Checkout page.
The default states are as follows:
1

cart

Initial state, you can treat it as a draft order
2

address

Buyer started the checkout process and is on the address step
3

delivery

Buyer has added Shipping and Billing addresses, now he or she needs to select a delivery option
4

payment

Buyer selected delivery option, now he or she needs to add a payment option
Only available if Order#payment_required? returns true. For most orders it will be true, but if the order is paid entirely with Store Credit it will be false.
5

confirm

Buyer added payment option, now he or she needs to confirm the order
Only available if Order#confirmation_required? returns true
6

complete

Order was placed. It will set the completed_at date to the current time.
Assuming that an order meets the criteria for the next state, you will be able to transition it to the next state by calling next on that object, eg.
If this returns false, then the order does not meet the criteria. To work out why it cannot transition, check the result of an errors method call.

Order Shipment states

Alongside the global Order state there’s also shipment_state column which indicates the state of all shipments. Order can have multiple shipments. For more on this please go to Shipment States page.

Order Payment states

Alongside the global Order state there’s also payment_state column which indicates the state of all payments. Order can have multiple payments. For more on this please go to Payment States page.

Line Items

Line items are used to keep track of items within the context of an order. These records provide a link between orders, and Variants.
When a variant is added to an order, the price of that item is tracked along with the line item to preserve that data. If the variant’s price were to change, then the line item would still have a record of the price at the time of ordering.

Addresses

An order can link to two Address objects. The shipping address indicates where the order’s product(s) should be shipped to. This address is used to determine which shipping methods are available for an order. The billing address indicates where the user who’s paying for the order is located. This can alter the tax rate for the order, which in turn can change how much the final order total can be. For more information about addresses, please read the Addresses guide.

Adjustments

Adjustments are used to affect an order’s final cost, either by decreasing it (Promotions) or by increasing it (Shipping, Taxes). For more information about adjustments, please see the Adjustments guide.

Payments

Payment records are used to track payment information about an order. Each Order can hold multiple payments, eg. one for each credit card or debit card and one for the store credit. For more information, please read the Payments guide.

Return Authorizations

An order can have many ReturnAuthorization objects. These records keeps track of which items have been authorized for return and how the user will be compensated — either via exchanging the item(s) or a reimbursement.

Updating an Order

If you change any aspect of an Order object within code and you wish to update the order’s totals — including associated adjustments and shipments — call the update_with_updater! method on that object, which calls out to the OrderUpdater class. For example, if you create or modify an existing payment for the order which would change the order’s payment_state to a different value, calling update_with_updater! will cause the payment_state to be recalculated for that order. Another example is if a LineItem within the order had its price changed. Calling update_with_updater! will cause the totals for the order to be updated, the adjustments for the order to be recalculated, and then a final total to be established.