Skip to main content
Customers represent the buyers in a store, kept separate from the application’s User model.

Overview

Lunar uses a dedicated Customer model to store customer details, rather than the User model. This keeps the application’s User models untouched and provides greater flexibility.

Customers

Creating a customer

Accessors

The Customer model provides a full_name accessor that combines the title, first_name, and last_name fields.

Relationships

Users

Customers are typically associated with a user so that they can place orders. It is also possible to have multiple users associated with a single customer. This is useful in B2B e-commerce where a customer may have multiple buyers.

Attaching users to a customer

Attaching a customer to a customer group

Customer Groups

Default retail Customer groups allow customers to be organized into logical segments, enabling different criteria to be defined on models based on which group a customer belongs to. These criteria include:

Pricing

Different pricing can be specified per customer group. For example, customers in the trade customer group may have different prices than those in retail.

Product Availability

Product visibility can be toggled depending on the customer group, meaning only certain products will show depending on the group a customer belongs to. Scheduling availability is also supported, allowing products to be released earlier or later to different groups.
At least one customer group must exist in the store. When Lunar is installed, a default group named retail is created.

Creating a customer group

Only one default customer group can exist at a time. If a new customer group is created with default set to true, the existing default will be set to false.

Scheduling availability

To add customer group availability to custom models, the HasCustomerGroups trait can be used.
This provides access to the following methods:

Scheduling customer groups

Unscheduling customer groups

To disable a model for a customer group, it can be unscheduled. This sets enabled to false and clears the starts_at and ends_at dates.

Parameters

Pivot Data By default the following values are used for $pivotData:
  • enabled - Whether the customer group is enabled. Defaults to true when scheduling and false when unscheduling.
  • starts_at - When scheduling, set to the $starts value. When unscheduling, set to null.
  • ends_at - When scheduling, set to the $ends value. When unscheduling, set to null.
Any of these values can be overridden, as they are merged internally.

Querying by customer group

The HasCustomerGroups trait adds a customerGroup scope to the model. This allows querying based on availability for one or more customer groups. The scope accepts a CustomerGroup model instance, an array, or a collection.
The $startsAt and $endsAt parameters can optionally be passed to filter by scheduling window. Both should be DateTime instances.
  • If neither is provided, both default to now() (with $endsAt set one second ahead).
  • The scope returns models where starts_at is null or before the given start, ends_at is null or after the given end, and either enabled or visible is true.
A model will only be returned if the enabled or visible column is true on the pivot, regardless of whether the start and end dates match.

Limit by customer group

Eloquent models that use the HasCustomerGroups trait have a useful scope available: