Model
The model used isShopper\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: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).Zones
Collections can be scoped to specific geographic zones. This is useful when a promotion or product group only applies to certain markets.Rules (Automatic Collections)
Automatic collections use rules to determine which products belong to them. Each rule defines a condition that products must match.Slug & Lookup
TheHasSlug trait generates unique slugs automatically and provides a findBySlug() static method:
Query Scopes
Filter collections by type: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
ThefirstRule() 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:Retrieving Products
Shopper provides built-in methods to retrieve products from collections, handling both manual and automatic collections transparently.Basic Usage
ThegetProducts() method works for both collection types. For manual collections it returns attached products. For automatic collections it evaluates the rules and returns matching products.
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
TheCollectionProductsQuery class handles rule evaluation for all supported rules:
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 theSyncCollectionProductsAction:
Background Jobs
Sync operations run in queued jobs to avoid blocking requests:SyncCollectionProductsJob- Syncs a single collectionSyncProductWithCollectionsJob- Syncs a product with all automatic collections
Media
Collections support two media collections through Spatie MediaLibrary, using the same config-driven collection names as products.
To add a collection cover image:
Configuration
Disabling Collections
Permissions
The admin panel generates five permissions for collection management:Components
To customize the admin UI for collection management:config/shopper/components/collection.php: