Skip to content

Commit 52604a6

Browse files
MC-24023: Placing order in Admin for more stock than is available is allowed and does not show error
1 parent f3df323 commit 52604a6

File tree

6 files changed

+40
-10
lines changed

6 files changed

+40
-10
lines changed

app/code/Magento/CatalogInventory/Model/Quote/Item/QuantityValidator/Initializer/StockItem.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ public function initialize(
118118
$product->getStore()->getWebsiteId()
119119
);
120120

121+
if ($result->getHasError() === true && in_array($result->getErrorCode(), ['qty_available', 'out_stock'])) {
122+
$quoteItem->setHasError(true);
123+
}
124+
121125
/* We need to ensure that any possible plugin will not erase the data */
122126
$backOrdersQty = $this->stockStateProvider->checkQuoteItemQty($stockItem, $rowQty, $qtyForCheck, $qty)
123127
->getItemBackorders();

app/code/Magento/CatalogInventory/Model/StockStateProvider.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Magento\Framework\Math\Division as MathDivision;
1515

1616
/**
17-
* Interface StockStateProvider
17+
* Provider stocks state
1818
*/
1919
class StockStateProvider implements StockStateProviderInterface
2020
{
@@ -156,6 +156,7 @@ public function checkQuoteItemQty(StockItemInterface $stockItem, $qty, $summaryQ
156156

157157
if (!$stockItem->getIsInStock()) {
158158
$result->setHasError(true)
159+
->setErrorCode('out_stock')
159160
->setMessage(__('This product is out of stock.'))
160161
->setQuoteMessage(__('Some of the products are out of stock.'))
161162
->setQuoteMessageIndex('stock');
@@ -165,7 +166,11 @@ public function checkQuoteItemQty(StockItemInterface $stockItem, $qty, $summaryQ
165166

166167
if (!$this->checkQty($stockItem, $summaryQty) || !$this->checkQty($stockItem, $qty)) {
167168
$message = __('The requested qty is not available');
168-
$result->setHasError(true)->setMessage($message)->setQuoteMessage($message)->setQuoteMessageIndex('qty');
169+
$result->setHasError(true)
170+
->setErrorCode('qty_available')
171+
->setMessage($message)
172+
->setQuoteMessage($message)
173+
->setQuoteMessageIndex('qty');
169174
return $result;
170175
} else {
171176
if ($stockItem->getQty() - $summaryQty < 0) {

app/code/Magento/CatalogInventory/etc/adminhtml/di.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
</argument>
1515
</arguments>
1616
</type>
17-
<type name="Magento\CatalogInventory\Model\Spi\StockStateProviderInterface">
18-
<arguments>
19-
<argument name="qtyCheckApplicable" xsi:type="boolean">false</argument>
20-
</arguments>
21-
</type>
2217
<type name="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider">
2318
<arguments>
2419
<argument name="addFieldStrategies" xsi:type="array">

app/code/Magento/ConfigurableProduct/Test/Mftf/Test/NoOptionAvailableToConfigureDisabledProductTest.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@
157157
<click selector="{{AdminInvoicePaymentShippingSection.shippingMethod}}" stepKey="chooseShippingMethod"/>
158158
<waitForPageLoad stepKey="waitForShippingMethodLoad"/>
159159
<click selector="{{AdminOrderFormActionSection.SubmitOrder}}" stepKey="clickSubmitOrder"/>
160-
<waitForPageLoad stepKey="waitForSuccess"/>
161-
<see selector="{{AdminOrderDetailsMessagesSection.successMessage}}" userInput="You created the order." stepKey="seeSuccessMessage"/>
160+
<waitForPageLoad stepKey="waitForError"/>
161+
<!-- Check that error remains -->
162+
<actionGroup ref="AssertAdminItemOrderedErrorActionGroup" stepKey="assertProductErrorRemains">
163+
<argument name="productName" value="$createConfigChildProduct2.name$"/>
164+
<argument name="messageType" value="error"/>
165+
<argument name="message" value="This product is out of stock."/>
166+
</actionGroup>
162167
</test>
163168
</tests>

app/code/Magento/Sales/Block/Adminhtml/Order/Create/Messages.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
*/
1515
class Messages extends \Magento\Framework\View\Element\Messages
1616
{
17+
18+
private const ITEMS_GRID = 'items_grid';
19+
1720
/**
1821
* Preparing global layout
1922
*
@@ -22,6 +25,24 @@ class Messages extends \Magento\Framework\View\Element\Messages
2225
protected function _prepareLayout()
2326
{
2427
$this->addMessages($this->messageManager->getMessages(true));
28+
$itemsBlock = $this->getLayout()->getBlock(self::ITEMS_GRID);
29+
if (!$itemsBlock) {
30+
return;
31+
}
32+
$items = $itemsBlock->getItems();
33+
foreach ($items as $item) {
34+
if ($item->getHasError()) {
35+
$messageCollection = $this->getMessageCollection();
36+
foreach ($messageCollection->getItems() as $blockMessage) {
37+
if ($item->getMessage(true) === $blockMessage->getText()) {
38+
/* Remove duplicated messages.*/
39+
$messageCollection->deleteMessageByIdentifier($blockMessage->getIdentifier());
40+
}
41+
}
42+
$this->setMessages($messageCollection);
43+
}
44+
}
45+
2546
parent::_prepareLayout();
2647
}
2748
}

app/code/Magento/Sales/Test/Mftf/Test/AdminAddSelectedProductToOrderTest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<!-- Check that error remains -->
7373
<actionGroup ref="AssertAdminItemOrderedErrorActionGroup" stepKey="assertProductErrorRemains">
7474
<argument name="productName" value="$simpleProduct.name$"/>
75-
<argument name="messageType" value="notice"/>
75+
<argument name="messageType" value="error"/>
7676
</actionGroup>
7777
</test>
7878
</tests>

0 commit comments

Comments
 (0)