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

Fix discounts that require a user to be logged in #3327

Merged
merged 8 commits into from
Dec 8, 2023
Merged
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 @@ -9,6 +9,7 @@
- Fixed a bug where sales weren’t respecting site element statuses. ([#3328](https://github.com/craftcms/commerce/issues/3328))
- Fixed a bug where soft-deleted order statuses and line item statuses would not being restored when applying project config changes. ([#3164](https://github.com/craftcms/commerce/issues/3164))
- Fixed a bug where carts weren’t being restored after logging in.
- Fixed a bug where guest customers could use a discount with a per-user usage limit. ([#3326](https://github.com/craftcms/commerce/issues/3326))
- Fixed a bug where orders with a processing transaction weren’t being completed.

## 4.3.2 - 2023-10-31
Expand Down
21 changes: 13 additions & 8 deletions src/services/Discounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,8 @@ public function orderCouponAvailable(Order $order, string &$explanation = null):
$explanation = Craft::t('commerce', 'Discount use has reached its limit.');
return false;
}

$user = $order->getCustomer();

if (!$this->_isDiscountPerUserUsageValid($discount, $user)) {

if (!$this->_isDiscountPerUserUsageValid($discount, $order->getCustomer())) {
$explanation = Craft::t('commerce', 'This coupon is for registered users and limited to {limit} uses.', [
'limit' => $discount->perUserLimit,
]);
Expand Down Expand Up @@ -600,13 +598,11 @@ public function matchOrder(Order $order, Discount $discount): bool
return false;
}

$user = $order->getCustomer();

if (!$this->_isDiscountTotalUseLimitValid($discount)) {
return false;
}

if (!$this->_isDiscountPerUserUsageValid($discount, $user)) {
if (!$this->_isDiscountPerUserUsageValid($discount, $order->getCustomer())) {
return false;
}

Expand Down Expand Up @@ -1124,6 +1120,15 @@ private function _isDiscountPerUserUsageValid(Discount $discount, ?User $user):
return false;
}

if (Craft::$app->getRequest()->getIsSiteRequest()) {
$currentUser = Craft::$app->getUser()->getIdentity();
$isCustomerCurrentUser = ($currentUser && $currentUser->id == $user->id);

if (!$isCustomerCurrentUser) {
return false;
}
}

$usage = (new Query())
->select(['uses'])
->from([Table::CUSTOMER_DISCOUNTUSES])
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/services/DiscountsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,9 +825,15 @@ protected function orderCouponAvailableTest(array $orderConfig, bool $desiredRes
protected function _before()
{
parent::_before();

$this->discounts = Plugin::getInstance()->getDiscounts();
$customerFixture = $this->tester->grabFixture('customers');
$this->_user = $customerFixture->getElement('customer1');
Craft::$app->getUser()->setIdentity($this->_user);
}

protected function _after()
{
Craft::$app->getUser()->setIdentity(null);
parent::_after();
}
}
Loading