Skip to content

Commit

Permalink
Merge pull request #180 from molnarerwin/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 committed Jul 4, 2024
2 parents 8308d5e + 114070f commit e1529ec
Show file tree
Hide file tree
Showing 3 changed files with 811 additions and 2 deletions.
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
49 changes: 47 additions & 2 deletions src/Foundation/Search/ProductSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ public function withinTaxon(Taxon $taxon): self
$query->where('id', $taxon->id);
});

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

return $this;
}
Expand All @@ -103,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 @@ -117,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 @@ -131,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 @@ -150,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 @@ -167,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 @@ -175,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 @@ -183,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 @@ -191,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 @@ -199,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 @@ -207,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 @@ -215,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 @@ -223,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 @@ -235,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 @@ -247,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 @@ -261,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 @@ -288,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 @@ -319,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 @@ -327,6 +371,7 @@ public function withChannels(): self
{
$this->productQuery->with('channels');
$this->masterProductQuery->with('channels');
$this->variantQuery?->with('masterProduct.channels');

return $this;
}
Expand Down
Loading

0 comments on commit e1529ec

Please sign in to comment.