Skip to content

Commit efd03a9

Browse files
committed
fix infinite loop
1 parent d925c75 commit efd03a9

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/base/Purchasable.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -829,18 +829,23 @@ function($attribute, $params, Validator $validator) use ($lineItem, $lineItemQua
829829
return;
830830
}
831831

832+
$lineItemPurchasable = $lineItem->getPurchasable();
833+
if (!$lineItemPurchasable instanceof Purchasable) {
834+
return;
835+
}
836+
832837
if (!$this->hasStock()) {
833-
if (!Plugin::getInstance()->getPurchasables()->isPurchasableOutOfStockPurchasingAllowed($lineItem->getPurchasable(), $lineItem->getOrder())) {
834-
$error = Craft::t('commerce', '“{description}” is currently out of stock.', ['description' => $lineItem->purchasable->getDescription()]);
838+
if (!Plugin::getInstance()->getPurchasables()->isPurchasableOutOfStockPurchasingAllowed($lineItemPurchasable, $lineItem->getOrder())) {
839+
$error = Craft::t('commerce', '“{description}” is currently out of stock.', ['description' => $lineItemPurchasable->getDescription()]);
835840
$validator->addError($lineItem, $attribute, $error);
836841
}
837842
}
838843

839844
$lineItemQty = $lineItem->id !== null ? $lineItemQuantitiesById[$lineItem->id] : $lineItemQuantitiesByPurchasableId[$lineItem->purchasableId];
840845

841846
if ($this->hasStock() && $this->inventoryTracked && $lineItemQty > $this->getStock()) {
842-
if (!Plugin::getInstance()->getPurchasables()->isPurchasableOutOfStockPurchasingAllowed($lineItem->getPurchasable(), $lineItem->getOrder())) {
843-
$error = Craft::t('commerce', 'There are only {num} “{description}” items left in stock.', ['num' => $this->getStock(), 'description' => $lineItem->purchasable->getDescription()]);
847+
if (!Plugin::getInstance()->getPurchasables()->isPurchasableOutOfStockPurchasingAllowed($lineItemPurchasable, $lineItem->getOrder())) {
848+
$error = Craft::t('commerce', 'There are only {num} “{description}” items left in stock.', ['num' => $this->getStock(), 'description' => $lineItemPurchasable->getDescription()]);
844849
$validator->addError($lineItem, $attribute, $error);
845850
}
846851
}

src/services/Purchasables.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,20 @@ class Purchasables extends Component
137137

138138

139139
/**
140-
* @param PurchasableInterface $purchasable
140+
* @param Purchasable $purchasable
141141
* @param Order|null $order
142142
* @param User|null $currentUser
143143
* @return bool
144144
* @throws Throwable
145145
* @since 5.3.0
146146
*/
147-
public function isPurchasableOutOfStockPurchasingAllowed(PurchasableInterface $purchasable, Order $order = null, User $currentUser = null): bool
147+
public function isPurchasableOutOfStockPurchasingAllowed(Purchasable $purchasable, Order $order = null, User $currentUser = null): bool
148148
{
149149
if ($currentUser === null) {
150150
$currentUser = Craft::$app->getUser()->getIdentity();
151151
}
152152

153-
$outOfStockPurchasesAllowed = $purchasable->getIsOutOfStockPurchasingAllowed();
153+
$outOfStockPurchasesAllowed = $purchasable->allowOutOfStockPurchases;
154154

155155
$event = new PurchasableOutOfStockPurchasesAllowedEvent(compact('order', 'purchasable', 'currentUser', 'outOfStockPurchasesAllowed'));
156156

0 commit comments

Comments
 (0)