Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat include variants in search #180

Open
wants to merge 1 commit into
base: feat-include-variants-in-search
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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