Skip to content

Commit

Permalink
Merge pull request #181 from vanilophp/feat-include-variants-in-search
Browse files Browse the repository at this point in the history
Feat include variants in search
  • Loading branch information
fulopattila122 authored Jul 4, 2024
2 parents c325159 + 02b5f11 commit 6fdfc49
Show file tree
Hide file tree
Showing 3 changed files with 843 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## Unreleased
##### 2024-XX-YY

- Added the `includeVariants()` method to the ProductSearch class
- Added the `priceGreaterThan`, `priceLessThan` and `priceBetween` methods to the ProductSearch class by [Matima](https://github.com/mahdirezaei-dev) in [#176](https://github.com/vanilophp/framework/pull/176)
- Added the `Macroable` trait to the `ProductSearch` class
- Added the unidirectional links feature
Expand Down
62 changes: 61 additions & 1 deletion src/Foundation/Search/ProductSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Vanilo\Channel\Contracts\Channel;
use Vanilo\MasterProduct\Contracts\MasterProduct;
use Vanilo\MasterProduct\Models\MasterProductProxy;
use Vanilo\MasterProduct\Models\MasterProductVariantProxy;
use Vanilo\Product\Contracts\Product;
use Vanilo\Product\Models\ProductProxy;
use Vanilo\Product\Models\ProductStateProxy;
Expand All @@ -42,6 +43,8 @@ class ProductSearch

protected Builder $masterProductQuery;

protected ?Builder $variantQuery = null;

protected ?string $orderBy = null;

public function __construct()
Expand Down Expand Up @@ -86,6 +89,12 @@ public function withinTaxon(Taxon $taxon): self
$query->where('id', $taxon->id);
});

$this->variantQuery?->whereHas('masterProduct', function ($query) use ($taxon) {
$query->whereHas('taxons', function ($query) use ($taxon) {
$query->where('id', $taxon->id);
});
});

return $this;
}

Expand All @@ -97,6 +106,11 @@ public function orWithinTaxon(Taxon $taxon): self
$this->masterProductQuery->orWhereHas('taxons', function ($query) use ($taxon) {
$query->where('id', $taxon->id);
});
$this->variantQuery?->orWhereHas('masterProduct', function ($query) use ($taxon) {
$query->whereHas('taxons', function ($query) use ($taxon) {
$query->where('id', $taxon->id);
});
});

return $this;
}
Expand All @@ -111,6 +125,11 @@ public function withinTaxons(array $taxons): self
$this->masterProductQuery->whereHas('taxons', function ($query) use ($taxonIds) {
$query->whereIn('id', $taxonIds);
});
$this->variantQuery?->whereHas('masterProduct', function ($query) use ($taxonIds) {
$query->whereHas('taxons', function ($query) use ($taxonIds) {
$query->whereIn('id', $taxonIds);
});
});

return $this;
}
Expand All @@ -125,6 +144,11 @@ public function orWithinTaxons(array $taxons): self
$this->masterProductQuery->orWhereHas('taxons', function ($query) use ($taxonIds) {
$query->whereIn('id', $taxonIds);
});
$this->variantQuery?->orWhereHas('masterProduct', function ($query) use ($taxonIds) {
$query->whereHas('taxons', function ($query) use ($taxonIds) {
$query->whereIn('id', $taxonIds);
});
});

return $this;
}
Expand All @@ -144,6 +168,11 @@ public function withinChannels(Channel ...$channels): self
$this->masterProductQuery->whereHas('channels', function ($query) use ($channelIds) {
$query->whereIn('channel_id', $channelIds);
});
$this->variantQuery?->whereHas('masterProduct', function ($query) use ($channelIds) {
$query->whereHas('channels', function ($query) use ($channelIds) {
$query->whereIn('channel_id', $channelIds);
});
});

return $this;
}
Expand All @@ -152,6 +181,7 @@ public function nameContains(string $term): self
{
$this->productQuery->where('name', 'like', "%$term%");
$this->masterProductQuery->where('name', 'like', "%$term%");
$this->variantQuery?->where('name', 'like', "%$term%");

return $this;
}
Expand All @@ -160,6 +190,7 @@ public function priceBetween(float $min, float $max): self
{
$this->productQuery->whereBetween('price', [$min, $max]);
$this->masterProductQuery->whereBetween('price', [$min, $max]);
$this->variantQuery?->whereBetween('price', [$min, $max]);

return $this;
}
Expand All @@ -168,6 +199,7 @@ public function priceGreaterThan(float $min): self
{
$this->productQuery->where('price', '>', $min);
$this->masterProductQuery->where('price', '>', $min);
$this->variantQuery?->where('price', '>', $min);

return $this;
}
Expand All @@ -176,6 +208,7 @@ public function priceGreaterThanOrEqualTo(float $min): self
{
$this->productQuery->where('price', '>=', $min);
$this->masterProductQuery->where('price', '>=', $min);
$this->variantQuery?->where('price', '>=', $min);

return $this;
}
Expand All @@ -184,6 +217,7 @@ public function priceLessThan(float $max): self
{
$this->productQuery->where('price', '<', $max);
$this->masterProductQuery->where('price', '<', $max);
$this->variantQuery?->where('price', '<', $max);

return $this;
}
Expand All @@ -192,6 +226,7 @@ public function priceLessThanOrEqualTo(float $max): self
{
$this->productQuery->where('price', '<=', $max);
$this->masterProductQuery->where('price', '<=', $max);
$this->variantQuery?->where('price', '<=', $max);

return $this;
}
Expand All @@ -200,6 +235,7 @@ public function nameStartsWith(string $term): self
{
$this->productQuery->where('name', 'like', "$term%");
$this->masterProductQuery->where('name', 'like', "$term%");
$this->variantQuery?->where('name', 'like', "$term%");

return $this;
}
Expand All @@ -208,6 +244,7 @@ public function orNameStartsWith(string $term): self
{
$this->productQuery->orWhere('name', 'like', "$term%");
$this->masterProductQuery->orWhere('name', 'like', "$term%");
$this->variantQuery?->orWhere('name', 'like', "$term%");

return $this;
}
Expand All @@ -216,6 +253,7 @@ public function nameEndsWith(string $term): self
{
$this->productQuery->where('name', 'like', "%$term");
$this->masterProductQuery->where('name', 'like', "%$term");
$this->variantQuery?->where('name', 'like', "%$term");

return $this;
}
Expand All @@ -228,6 +266,9 @@ public function havingPropertyValue(PropertyValue $propertyValue): self
$this->masterProductQuery->whereHas('propertyValues', function ($query) use ($propertyValue) {
$query->where('id', $propertyValue->id);
});
$this->variantQuery?->whereHas('propertyValues', function ($query) use ($propertyValue) {
$query->where('id', $propertyValue->id);
});

return $this;
}
Expand All @@ -240,6 +281,9 @@ public function orHavingPropertyValue(PropertyValue $propertyValue): self
$this->masterProductQuery->orWhereHas('propertyValues', function ($query) use ($propertyValue) {
$query->where('id', $propertyValue->id);
});
$this->variantQuery?->orWhereHas('propertyValues', function ($query) use ($propertyValue) {
$query->where('id', $propertyValue->id);
});

return $this;
}
Expand All @@ -254,6 +298,9 @@ public function havingPropertyValues(array $propertyValues): self
$this->masterProductQuery->whereHas('propertyValues', function ($query) use ($propertyValueIds) {
$query->whereIn('id', $propertyValueIds);
});
$this->variantQuery?->whereHas('propertyValues', function ($query) use ($propertyValueIds) {
$query->whereIn('id', $propertyValueIds);
});

return $this;
}
Expand Down Expand Up @@ -281,6 +328,9 @@ public function orHavingPropertyValues(array $propertyValues): self
$this->masterProductQuery->orWhereHas('propertyValues', function ($query) use ($propertyValueIds) {
$query->whereIn('id', $propertyValueIds);
});
$this->variantQuery?->orWhereHas('propertyValues', function ($query) use ($propertyValueIds) {
$query->whereIn('id', $propertyValueIds);
});

return $this;
}
Expand Down Expand Up @@ -312,6 +362,7 @@ public function withImages(): self
{
$this->productQuery->with('media');
$this->masterProductQuery->with(['media', 'variants.media']);
$this->variantQuery?->with('media');

return $this;
}
Expand All @@ -320,6 +371,7 @@ public function withChannels(): self
{
$this->productQuery->with('channels');
$this->masterProductQuery->with('channels');
$this->variantQuery?->with('masterProduct.channels');

return $this;
}
Expand All @@ -333,7 +385,8 @@ public function getSearcher(string|array $columns = null): Searcher

return $this->searcher
->add($this->productQuery, $columns, $orderBy)
->add($this->masterProductQuery, $columns, $orderBy);
->add($this->masterProductQuery, $columns, $orderBy)
->when(null !== $this->variantQuery, fn ($search) => $search->add($this->variantQuery));
}

public function simplePaginate(int $perPage = 15, array $columns = ['*'], string $pageName = 'page', int $page = null): Paginator
Expand All @@ -351,4 +404,11 @@ public function getResults(int $limit = null): Collection
{
return is_null($limit) ? $this->getSearcher()->search() : $this->getSearcher()->simplePaginate($limit)->search()->getCollection();
}

public function includeVariants(): self
{
$this->variantQuery = MasterProductVariantProxy::query();

return $this;
}
}
Loading

0 comments on commit 6fdfc49

Please sign in to comment.