Associations define relationships between products, such as cross-sells, up-sells, and alternates.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
Associations allow products to be related to each other. The type of association defines how the relationship should be presented on a storefront and how Lunar interprets it. Lunar ships with three built-in types (cross-sell, up-sell, and alternate), but custom types can also be created.Model
Associations are stored asLunar\Models\ProductAssociation models.
Fields
| Field | Type | Description |
|---|---|---|
id | bigIncrements | Primary key |
product_parent_id | foreignId | The owning product |
product_target_id | foreignId | The associated product |
type | string | The association type (e.g. cross-sell, up-sell, alternate) |
created_at | timestamp | |
updated_at | timestamp |
Relationships
| Relationship | Type | Related Model | Description |
|---|---|---|---|
parent | BelongsTo | Lunar\Models\Product | The owning product |
target | BelongsTo | Lunar\Models\Product | The associated product |
Scopes
| Scope | Description |
|---|---|
crossSell() | Filter to cross-sell associations |
upSell() | Filter to up-sell associations |
alternate() | Filter to alternate associations |
type(ProvidesProductAssociationType|string $type) | Filter by a specific type |
Association Types Enum
The built-in association types are defined using theLunar\Base\Enums\ProductAssociation enum:
Loading Associations
Lunar\Models\ProductAssociation models:
Inverse Associations
To find products that associate to a given product (i.e. where the product is the target), use theinverseAssociations relationship:
Types of Association
Cross-Sell
Cross-selling encourages customers to purchase complementary products in addition to the item they intended to buy. For example, if a store sells a phone, cross-sell associations could include headphones or a case that works with that phone. Adding a cross-sell associationUp-Sell
Up-selling encourages customers to upgrade or include add-ons to the product they are buying, typically to a higher-value option. For example, given two phones:- Phone 16GB 5” Screen
- Phone 32GB 6” Screen
Alternate
Alternate products are alternatives to the current product. This is useful when a product is out of stock or not quite the right fit, allowing the storefront to suggest similar options. Adding an alternate associationCustom Types
In addition to the built-in types, custom association types can be defined by passing any string as the type. No registration or configuration is required since thetype column is a plain string in the database.
type scope:
Removing Associations
Thedissociate method removes associations between products. It accepts a single product, an array, or a collection. If no type is specified, all association types for the given product(s) are removed.
Queued Operations
Both
associate() and dissociate() dispatch queued jobs (Lunar\Jobs\Products\Associations\Associate and Lunar\Jobs\Products\Associations\Dissociate). This means the changes may not be reflected immediately if using an asynchronous queue driver.