Skip to content

Commit

Permalink
Added the ability to scope customer groups to retrieve only items tha…
Browse files Browse the repository at this point in the history
…t are visible
  • Loading branch information
chrispage1 committed Jan 23, 2025
1 parent 141aa21 commit 15b191d
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions packages/core/src/Base/Traits/HasCustomerGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ public static function bootHasCustomerGroups(): void
static::created(function (Model $model) {
$model->customerGroups()->sync(
CustomerGroup::get()->mapWithKeys(
fn ($customerGroup): array => [$customerGroup->id => [
'enabled' => $customerGroup->default,
'starts_at' => now(),
'ends_at' => null,
'visible' => $customerGroup->default,
...static::getExtraCustomerGroupPivotValues($customerGroup),
]]
fn ($customerGroup): array => [
$customerGroup->id => [
'enabled' => $customerGroup->default,
'starts_at' => now(),
'ends_at' => null,
'visible' => $customerGroup->default,
...static::getExtraCustomerGroupPivotValues($customerGroup),
],
]
)
);
});
Expand Down Expand Up @@ -94,13 +96,20 @@ protected function validateScheduling(Collection $models)
* @param Collection $customerGroups
* @return Builder
*/
public function applyCustomerGroupScope(Builder $query, Collection $groupIds, DateTime $startsAt, DateTime $endsAt)
{
return $query->whereHas('customerGroups', function ($relation) use ($groupIds, $startsAt, $endsAt) {
public function applyCustomerGroupScope(
Builder $query,
Collection $groupIds,
DateTime $startsAt,
DateTime $endsAt,
bool $onlyVisible
) {
return $query->whereHas('customerGroups', function ($relation) use ($groupIds, $startsAt, $endsAt, $onlyVisible) {
$relation->whereIn(
$this->customerGroups()->getTable().'.customer_group_id',
$groupIds
)->where(function ($query) use ($startsAt) {
)->when($onlyVisible, function ($query) {
$query->where('visible', true);
})->where(function ($query) use ($startsAt) {
$query->whereNull('starts_at')
->orWhere('starts_at', '<=', $startsAt);
})->where(function ($query) use ($endsAt) {
Expand All @@ -120,8 +129,13 @@ public function applyCustomerGroupScope(Builder $query, Collection $groupIds, Da
* @param CustomerGroup|string $customerGroup
* @return Builder
*/
public function scopeCustomerGroup($query, CustomerGroup|iterable|null $customerGroup = null, ?DateTime $startsAt = null, ?DateTime $endsAt = null)
{
public function scopeCustomerGroup(
$query,
CustomerGroup|iterable|null $customerGroup = null,
?DateTime $startsAt = null,
?DateTime $endsAt = null,
?bool $onlyVisible = false
) {
if (blank($customerGroup)) {
return $query;
}
Expand All @@ -148,6 +162,6 @@ public function scopeCustomerGroup($query, CustomerGroup|iterable|null $customer
$endsAt = now()->addSecond();
}

return $this->applyCustomerGroupScope($query, $groupIds, $startsAt, $endsAt);
return $this->applyCustomerGroupScope($query, $groupIds, $startsAt, $endsAt, $onlyVisible);
}
}

0 comments on commit 15b191d

Please sign in to comment.