This commit introduces a RecommendationService that is used when indexing products. The service is utilizing a recommendation rules interface, so many rules can be created and applied during indexing the products
28 lines
905 B
PHP
28 lines
905 B
PHP
<?php
|
|
|
|
namespace Modules\Core\Catalog\Recommendations;
|
|
|
|
use Illuminate\Support\Collection;
|
|
use Lunar\Models\Product;
|
|
use Modules\Core\Catalog\Contracts\RecommendationRule;
|
|
|
|
/**
|
|
* The universal fallback — always returns something as long as the store
|
|
* has more than one product, since it has no eligibility condition of its
|
|
* own to come up empty on. Meant to be placed last in
|
|
* config('catalog.recommendation_rules'), not first: every store using
|
|
* the default chain gets a real fallback, but one that only kicks in once
|
|
* more specific rules (same category, same tag, ...) have had a chance.
|
|
*/
|
|
class RandomRule implements RecommendationRule
|
|
{
|
|
public function recommend(Product $product, int $limit, array $exclude): Collection
|
|
{
|
|
return Product::query()
|
|
->whereKeyNot($exclude)
|
|
->inRandomOrder()
|
|
->limit($limit)
|
|
->get();
|
|
}
|
|
}
|