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 caching bug when calculating available discounts #3902

Open
wants to merge 4 commits into
base: 5.x
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Craft Commerce

## Unreleased

- Fixed a bug where discounts could not be applied when the order is recalculated more than once in the same request. ([#3896](https://github.com/craftcms/commerce/issues/3896))

## 5.3.3 - 2025-02-19

- Fixed a bug where line item totals could be formatted in the wrong currency on Edit Order pages. ([#3891](https://github.com/craftcms/commerce/issues/3891))
Expand Down
4 changes: 4 additions & 0 deletions src/adjusters/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public function adjust(Order $order): array
}
}

if (!$availableDiscounts) {
return [];
}

foreach ($this->_order->getLineItems() as $lineItem) {
$lineItemHashId = spl_object_hash($lineItem);
$lineItemDiscountAmount = $lineItem->getDiscount();
Expand Down
38 changes: 27 additions & 11 deletions src/services/Discounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,23 @@ public function getAllActiveDiscounts(?Order $order = null): array
$dateKey = DateTimeHelper::toIso8601($date);
$storeKey = $order ? $order->getStore()->id : '*';
$purchasablesKey = !empty($purchasableIds) ? md5(serialize($purchasableIds)) : '';
$cacheKey = implode(':', array_filter([$dateKey, $couponKey, $storeKey, $purchasablesKey]));
$itemSubtotal = $order ? $order->getItemSubtotal() : '*';
$orderTotalQty = $order ? $order->getTotalQty() : '*';
$orderEmail = $order ? $order->getEmail() : '*';
$cacheKey = implode(':', array_filter([
$dateKey,
$couponKey,
$storeKey,
$purchasablesKey,
$itemSubtotal,
$orderTotalQty,
$orderEmail,
]));

if (isset($this->_activeDiscountsByKey[$cacheKey])) {
return $this->_activeDiscountsByKey[$cacheKey];
$cacheKeyMd5 = md5($cacheKey);

if (isset($this->_activeDiscountsByKey[$cacheKeyMd5])) {
return $this->_activeDiscountsByKey[$cacheKeyMd5];
}

$discountQuery = $this->_createDiscountQuery()
Expand Down Expand Up @@ -330,12 +343,12 @@ public function getAllActiveDiscounts(?Order $order = null): array

// Pre-qualify discounts based on purchase total
if ($order) {
if ($order->getEmail()) {
if ($orderEmail) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this check if $orderEmail !== '*' based on the code further up? Either that or leave this code as $order->getEmail()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I suppose it is already checking the $order variable so should be fine

$emailUsesSubQuery = (new Query())
->select([new Expression('COALESCE(SUM([[edu.uses]]), 0)')])
->from(['edu' => Table::EMAIL_DISCOUNTUSES])
->where(new Expression('[[edu.discountId]] = [[discounts.id]]'))
->andWhere(['email' => $order->getEmail()]);
->andWhere(['email' => $orderEmail]);

$discountQuery->andWhere([
'or',
Expand All @@ -346,20 +359,21 @@ public function getAllActiveDiscounts(?Order $order = null): array
$discountQuery->andWhere(['perEmailLimit' => 0]);
}


$discountQuery->andWhere([
'or',
['purchaseTotal' => 0],
['and', ['allPurchasables' => true], ['allCategories' => true], ['<=', 'purchaseTotal', $order->getItemSubtotal()]],
['and', ['allPurchasables' => true], ['allCategories' => true], ['<=', 'purchaseTotal', $itemSubtotal]],
['allPurchasables' => false],
['allCategories' => false],
]);

$discountQuery->andWhere([
'or',
['purchaseQty' => 0, 'maxPurchaseQty' => 0],
['and', ['allPurchasables' => true], ['allCategories' => true], ['>', 'purchaseQty', 0], ['maxPurchaseQty' => 0], ['<=', 'purchaseQty', $order->getTotalQty()]],
['and', ['allPurchasables' => true], ['allCategories' => true], ['>', 'maxPurchaseQty', 0], ['purchaseQty' => 0], ['>=', 'maxPurchaseQty', $order->getTotalQty()]],
['and', ['allPurchasables' => true], ['allCategories' => true], ['>', 'maxPurchaseQty', 0], ['>', 'purchaseQty', 0], ['<=', 'purchaseQty', $order->getTotalQty()], ['>=', 'maxPurchaseQty', $order->getTotalQty()]],
['and', ['allPurchasables' => true], ['allCategories' => true], ['>', 'purchaseQty', 0], ['maxPurchaseQty' => 0], ['<=', 'purchaseQty', $orderTotalQty]],
['and', ['allPurchasables' => true], ['allCategories' => true], ['>', 'maxPurchaseQty', 0], ['purchaseQty' => 0], ['>=', 'maxPurchaseQty', $orderTotalQty]],
['and', ['allPurchasables' => true], ['allCategories' => true], ['>', 'maxPurchaseQty', 0], ['>', 'purchaseQty', 0], ['<=', 'purchaseQty', $orderTotalQty], ['>=', 'maxPurchaseQty', $orderTotalQty]],
['allPurchasables' => false],
['allCategories' => false],
]);
Expand Down Expand Up @@ -419,9 +433,11 @@ public function getAllActiveDiscounts(?Order $order = null): array
);
}

$this->_activeDiscountsByKey[$cacheKey] = $this->_populateDiscounts($discountQuery->all());
$discountResults = $discountQuery->all();
$discounts = $this->_populateDiscounts($discountResults);
$this->_activeDiscountsByKey[$cacheKeyMd5] = $discounts;

return $this->_activeDiscountsByKey[$cacheKey];
return $this->_activeDiscountsByKey[$cacheKeyMd5];
}

/**
Expand Down