Skip to content

Commit

Permalink
Fix issue when discount relation dates are null (#797)
Browse files Browse the repository at this point in the history
* Fix issue when dates are null

* Bug fix on logic

* Change test as NULL is a valid starts_at

Co-authored-by: Alec Ritson <hello@itsalec.co.uk>
  • Loading branch information
ryanmitchell and alecritson authored Jan 12, 2023
1 parent 17bba79 commit d945e4b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
16 changes: 10 additions & 6 deletions packages/core/src/Managers/DiscountManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,26 @@ public function getDiscounts(): Collection
$joinTable = (new Discount)->channels()->getTable();
$query->whereIn("{$joinTable}.channel_id", $this->channels->pluck('id'))
->where("{$joinTable}.enabled", true)
->whereNotNull("{$joinTable}.starts_at")
->where("{$joinTable}.starts_at", '<=', now())
->where(function ($query) use ($joinTable) {
$query->whereNull("{$joinTable}.starts_at")
->orWhere("{$joinTable}.starts_at", '<=', now());
})
->where(function ($query) use ($joinTable) {
$query->whereNull("{$joinTable}.ends_at")
->orWhereDate("{$joinTable}.ends_at", '>', now());
->orWhere("{$joinTable}.ends_at", '>', now());
});
})->whereHas('customerGroups', function ($query) {
$joinTable = (new Discount)->customerGroups()->getTable();

$query->whereIn("{$joinTable}.customer_group_id", $this->customerGroups->pluck('id'))
->where("{$joinTable}.enabled", true)
->whereNotNull("{$joinTable}.starts_at")
->where("{$joinTable}.starts_at", '<=', now())
->where(function ($query) use ($joinTable) {
$query->whereNull("{$joinTable}.starts_at")
->orWhere("{$joinTable}.starts_at", '<=', now());
})
->where(function ($query) use ($joinTable) {
$query->whereNull("{$joinTable}.ends_at")
->orWhereDate("{$joinTable}.ends_at", '>', now());
->orWhere("{$joinTable}.ends_at", '>', now());
});
})->orderBy('priority')->get();
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/tests/Unit/Managers/DiscountManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,17 @@ public function can_restrict_discounts_to_customer_group()
]);

$this->assertEmpty($manager->getDiscounts());

$discount->customerGroups()->sync([
$customerGroup->id => [
'enabled' => true,
'visible' => true,
'starts_at' => now(),
'starts_at' => now()->addMinutes(1),
],
$customerGroupTwo->id => [
'enabled' => true,
'visible' => false,
'starts_at' => null,
'starts_at' => now()->addMinutes(1),
],
]);

Expand Down

0 comments on commit d945e4b

Please sign in to comment.