Customers represent the buyers in a store, kept separate from the application’s User model.Documentation Index
Fetch the complete documentation index at: https://docs.lunarphp.com/llms.txt
Use this file to discover all available pages before exploring further.
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
| Field | Type | Description |
|---|---|---|
id | primary key | |
title | string nullable | Salutation e.g. Mr, Mrs, Miss |
first_name | string | |
last_name | string | |
company_name | string nullable | |
tax_identifier | string nullable | |
account_ref | string nullable | |
attribute_data | json nullable | |
meta | json nullable | |
created_at | timestamp | |
updated_at | timestamp |
Creating a customer
Accessors
The Customer model provides afull_name accessor that combines the title, first_name, and last_name fields.
Relationships
| Relationship | Type | Related Model | Description |
|---|---|---|---|
customerGroups | BelongsToMany | Lunar\Models\CustomerGroup | Pivot: customer_customer_group |
users | BelongsToMany | Configured via auth.providers.users.model | Pivot: customer_user |
discounts | BelongsToMany | Lunar\Models\Discount | Pivot: customer_discount |
addresses | HasMany | Lunar\Models\Address | |
orders | HasMany | Lunar\Models\Order | |
mappedAttributes | MorphToMany | Lunar\Models\Attribute |
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
Defaultretail
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 thetrade 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
Scheduling availability
To add customer group availability to custom models, theHasCustomerGroups trait can be used.
Scheduling customer groups
Unscheduling customer groups
To disable a model for a customer group, it can be unscheduled. This setsenabled to false and clears the starts_at and ends_at dates.
Parameters
| Field | Description | Type |
|---|---|---|
$models | A CustomerGroup model, array, or collection of CustomerGroup models. | mixed |
$starts | The date the customer group will be active from. | DateTime|null |
$ends | The date the customer group will be active until. | DateTime|null |
$pivotData | Any additional pivot data on the link table. | array |
$pivotData:
enabled- Whether the customer group is enabled. Defaults totruewhen scheduling andfalsewhen unscheduling.starts_at- When scheduling, set to the$startsvalue. When unscheduling, set tonull.ends_at- When scheduling, set to the$endsvalue. When unscheduling, set tonull.
Querying by customer group
TheHasCustomerGroups 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.
$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$endsAtset one second ahead). - The scope returns models where
starts_atisnullor before the given start,ends_atisnullor after the given end, and eitherenabledorvisibleistrue.
Limit by customer group
Eloquent models that use theHasCustomerGroups trait have a useful scope available: