Skip to main content
Collections are curated groups of products for marketing and merchandising. Unlike categories, collections can be manually curated or automatically populated based on rules.

Model

The model used is Shopper\Models\Collection, which extends Shopper\Core\Models\Collection. The core model provides the business logic, relationships, scopes, rule evaluation, and slug generation. The admin model adds media collections and conversions through Spatie MediaLibrary. The core model implements Shopper\Core\Models\Contracts\Collection and uses the HasSlug trait for automatic slug generation with collision handling.

Extending the Model

To add custom behavior, extend the admin model and update your configuration:
Update config/shopper/models.php:

Database Schema

Collection Table

CollectionRule Table

Collection Types

Type Checking

Relationships

Products

A collection groups products together for merchandising, such as “Summer Essentials”, “New Arrivals”, or “Black Friday Deals”. For manual collections, you explicitly attach and detach products. For automatic collections, products are matched by rules (see Collection Rules).
For manual collections, manage products with the standard Eloquent relationship methods:

Zones

Collections can be scoped to specific geographic zones. This is useful when a promotion or product group only applies to certain markets.
From the zone side, you can retrieve all associated collections:

Rules (Automatic Collections)

Automatic collections use rules to determine which products belong to them. Each rule defines a condition that products must match.
Add a rule that matches products whose title contains “summer”:

Slug & Lookup

The HasSlug trait generates unique slugs automatically and provides a findBySlug() static method:

Query Scopes

Filter collections by type:
The published scope filters collections that have a published_at date in the past or present:

Collection Rules

Rule Types

Price rules (ProductPrice, CompareAtPrice) store values in cents. Pass the value in cents directly (e.g., '5000' for $50.00). Price rules are evaluated against the shop’s default currency.Sales count (ProductSalesCount) only counts units from orders with a valid status: paid, shipped, delivered, or completed.

Operators

Match Conditions

When a collection has multiple rules, match_conditions determines how they combine. With all, every rule must match. With any, a single matching rule is enough.

Display Helpers

The firstRule() method returns a human-readable summary of the collection’s rules, like “Product title contains Summer + 2 other”:

Creating Collections

Manual Collection

Automatic Collection

Automatic collections populate themselves based on rules. Here’s a “Sale Items” collection that matches products under $50 with “sale” in the title:

Best Sellers Collection

A collection that automatically includes products with more than 10 units sold:

Top Rated Collection

Retrieving Collections

All published collections:
A collection with its published products:
Filter by type:

Retrieving Products

Shopper provides built-in methods to retrieve products from collections, handling both manual and automatic collections transparently.

Basic Usage

The getProducts() method works for both collection types. For manual collections it returns attached products. For automatic collections it evaluates the rules and returns matching products.
For pagination or additional filtering, use productsQuery() which returns an Eloquent builder:

How It Works

For manual collections, getProducts() returns the attached products via the products() relationship. For automatic collections, getProducts() evaluates all defined rules and returns matching products:
  • Match All: All rules must be satisfied (AND logic)
  • Match Any: At least one rule must be satisfied (OR logic)

Rule Evaluation

The CollectionProductsQuery class handles rule evaluation for all supported rules:
For further customization, use the query builder:

Automatic Synchronization

For automatic collections, Shopper keeps the product associations in sync with the collection rules. Products are stored in the pivot table for optimal query performance.

How Sync Works

Synchronization happens automatically via observers:

Manual Sync Command

You can manually sync automatic collections using the artisan command:

Sync Action

For programmatic sync, use the SyncCollectionProductsAction:

Background Jobs

Sync operations run in queued jobs to avoid blocking requests:
  • SyncCollectionProductsJob - Syncs a single collection
  • SyncProductWithCollectionsJob - Syncs a product with all automatic collections
Configure your queue worker to process these jobs:

Media

Collections support two media collections through Spatie MediaLibrary, using the same config-driven collection names as products. To add a collection cover image:
To retrieve the cover URL:

Configuration

Disabling Collections

Permissions

The admin panel generates five permissions for collection management:

Components

To customize the admin UI for collection management:
Creates config/shopper/components/collection.php:

Storefront Example

Use Cases