diff --git a/Changelog.md b/Changelog.md index 39b3fc29..0878495d 100644 --- a/Changelog.md +++ b/Changelog.md @@ -19,6 +19,7 @@ - Added the `mapInto()` method to the `RelationAdjustmentCollection` class, which forwards the call to the underlying Eloquent collection - Added the `Channelable` behavior to Foundation Product, MasterProduct and Taxonomy classes - Added and extended Foundation Channel model that contains the known relationships to the "channelable" models +- Added the `withinChannel[s]` methods to the ProductSearch class ## 3.x Series diff --git a/src/Foundation/Search/ProductSearch.php b/src/Foundation/Search/ProductSearch.php index acbcaa5e..4507cc9b 100644 --- a/src/Foundation/Search/ProductSearch.php +++ b/src/Foundation/Search/ProductSearch.php @@ -21,6 +21,7 @@ use Illuminate\Support\Collection; use Konekt\Search\Facades\Search; use Konekt\Search\Searcher; +use Vanilo\Channel\Contracts\Channel; use Vanilo\Foundation\Models\Taxon; use Vanilo\MasterProduct\Contracts\MasterProduct; use Vanilo\MasterProduct\Models\MasterProductProxy; @@ -123,6 +124,25 @@ public function orWithinTaxons(array $taxons): self return $this; } + public function withinChannel(Channel $channel): self + { + return $this->withinChannels($channel); + } + + public function withinChannels(Channel ...$channels): self + { + $channelIds = collect($channels)->pluck('id'); + + $this->productQuery->whereHas('channels', function ($query) use ($channelIds) { + $query->whereIn('id', $channelIds); + }); + $this->masterProductQuery->whereHas('channels', function ($query) use ($channelIds) { + $query->whereIn('id', $channelIds); + }); + + return $this; + } + public function nameContains(string $term): self { $this->productQuery->where('name', 'like', "%$term%");