Skip to main content
Orders represent completed or in-progress purchases, created when a cart is converted at checkout.

Overview

Orders represent completed or in-progress purchases in a store. Orders are linked to carts, and although there is generally only one order per cart, the system supports multiple orders per cart if needed.
All monetary values (such as sub_total, total, tax_total) are cast to Lunar\DataTypes\Price objects, providing access to value, formatted(), and decimal properties.

Fields

Relationships

Creating an Order

An order can be created directly or, the recommended approach, via a Lunar\Models\Cart model.
  • allowMultipleOrders - Carts generally only have one draft order associated. Pass true to allow multiple orders per cart.
  • orderIdToUpdate - Optionally pass the ID of an existing draft order to update instead of creating a new one. The order must have a null placed_at value and belong to the cart.
The underlying class for creating an order is Lunar\Actions\Carts\CreateOrder. This can be overridden in config/lunar/cart.php:
At minimum, a custom class should extend Lunar\Actions\AbstractAction:

Validating a Cart Before Creation

To check whether a cart is ready to create an order:
This uses the Lunar\Validation\Cart\ValidateCartForOrderCreation class, which throws validation exceptions with helpful messages if the cart is not ready. A custom validation class can be specified in config/lunar/cart.php:
A custom validator should extend Lunar\Validation\BaseValidator:

Order Reference Generation

By default, Lunar generates an order reference when creating an order from a cart. The format is:
{0..0} indicates the order ID is padded to 8 digits (not including the prefix). The prefix is optional and defined in config/lunar/orders.php.

Custom Generators

To use a custom reference generator, update config/lunar/orders.php:
To disable reference generation entirely (not recommended), set the value to null. A custom generator must implement Lunar\Base\OrderReferenceGeneratorInterface:

Modifying Orders

To programmatically change order values or add new behavior, the order system can be extended. See Order Modifiers for more details.

Order Status

The placed_at field determines whether an order is considered draft or placed. The Lunar\Models\Order model provides two helper methods:

Order Lines

Fields

Relationships

Creating an Order Line

When using the createOrder method on a cart, order lines are created automatically.

Order Addresses

An order can have many addresses, typically one for billing and one for shipping.
When using the createOrder method on a cart, order addresses are created automatically.

Fields

Relationships

Creating an Order Address

The shipping and billing addresses can be accessed directly:

Shipping Options

A Shipping Tables add-on is planned to simplify shipping configuration in the admin panel.
To add shipping options, extend Lunar with custom logic. Shipping options can be fetched using the ShippingManifest facade:
This returns a collection of Lunar\DataTypes\ShippingOption objects.

Adding a Shipping Option to the Cart

Once a shipping option has been selected, add it to the cart so totals can be recalculated:

Transactions

Fields

Relationships

Creating a Transaction

An order having transactions does not mean it has been placed. Lunar determines whether an order is placed based on whether the placed_at column has a datetime value, regardless of any transactions.
Most stores will want to store transactions against orders to track how much has been paid, the payment method used, and how to issue refunds if needed.
Transactions can be retrieved via relationships:

Payments

Lunar is payment-provider agnostic. Any payment provider can be integrated with a storefront. The key factor for an order is whether the placed_at column is populated. Everything else about payment handling is left to the store implementation. Lunar provides helper utilities (as described above) to manage the payment lifecycle.

Order Notifications

Lunar allows specifying which Laravel mailers and notifications should be available when updating an order’s status. These are configured in config/lunar/orders.php:
When updating an order’s status in the admin panel, any configured mailers for the new status are available to select. Email addresses can be chosen, and additional addresses can be added. Lunar stores a render of the sent email in the activity log, providing a clear history of communications.
These email notifications are not sent automatically when updating the status programmatically outside of the admin panel.

Mailer Template

When building a mailer template, the $order model is available in the view data. When the status is updated, the order is passed through along with any additional content entered. Since additional content may not always be present, check for its existence first. Example template:

Order Invoice PDF

By default, clicking “Download PDF” in the admin panel when viewing an order generates a basic PDF. The view powering this PDF can be published for customization:
This creates a view at resources/vendor/lunarpanel/pdf/order.blade.php that can be freely customized.