Skip to content

Commit d0e44f9

Browse files
author
Oleksii Korshenko
authored
Merge pull request #365 from magento-fearless-kiwis/FearlessKiwis-MAGETWO-55287-Missing-prod-name-in-bundle-error-message
Fixed issue: - MAGETWO-55287: Missing product name in error message for bundle product
2 parents fdfe68f + c2359c9 commit d0e44f9

File tree

5 files changed

+677
-49
lines changed

5 files changed

+677
-49
lines changed

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

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@
99

1010
use Magento\CatalogInventory\Api\StockRegistryInterface;
1111
use Magento\CatalogInventory\Api\StockStateInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\CatalogInventory\Api\Data\StockItemInterface;
14+
use Magento\CatalogInventory\Helper\Data;
15+
use Magento\CatalogInventory\Model\Quote\Item\QuantityValidator\Initializer\Option;
16+
use Magento\CatalogInventory\Model\Quote\Item\QuantityValidator\Initializer\StockItem;
17+
use Magento\Framework\Event\Observer;
1218

19+
/**
20+
* Class QuantityValidator
21+
*/
1322
class QuantityValidator
1423
{
1524
/**
@@ -23,24 +32,25 @@ class QuantityValidator
2332
protected $stockItemInitializer;
2433

2534
/**
26-
* @var StockRegistryInterface
35+
* @var \Magento\CatalogInventory\Api\StockRegistryInterface
2736
*/
2837
protected $stockRegistry;
2938

3039
/**
31-
* @var StockStateInterface
40+
* @var \Magento\CatalogInventory\Api\StockStateInterface
3241
*/
3342
protected $stockState;
3443

3544
/**
36-
* @param QuantityValidator\Initializer\Option $optionInitializer
37-
* @param QuantityValidator\Initializer\StockItem $stockItemInitializer
45+
* @param Option $optionInitializer
46+
* @param StockItem $stockItemInitializer
3847
* @param StockRegistryInterface $stockRegistry
3948
* @param StockStateInterface $stockState
49+
* @return void
4050
*/
4151
public function __construct(
42-
QuantityValidator\Initializer\Option $optionInitializer,
43-
QuantityValidator\Initializer\StockItem $stockItemInitializer,
52+
Option $optionInitializer,
53+
StockItem $stockItemInitializer,
4454
StockRegistryInterface $stockRegistry,
4555
StockStateInterface $stockState
4656
) {
@@ -50,6 +60,30 @@ public function __construct(
5060
$this->stockState = $stockState;
5161
}
5262

63+
/**
64+
* Add error information to Quote Item
65+
*
66+
* @param \Magento\Framework\DataObject $result
67+
* @param \Magento\Quote\Model\Quote\Item $quoteItem
68+
* @param bool $removeError
69+
* @return void
70+
*/
71+
private function addErrorInfoToQuote($result, $quoteItem)
72+
{
73+
$quoteItem->addErrorInfo(
74+
'cataloginventory',
75+
Data::ERROR_QTY,
76+
$result->getMessage()
77+
);
78+
79+
$quoteItem->getQuote()->addErrorInfo(
80+
$result->getQuoteMessageIndex(),
81+
'cataloginventory',
82+
Data::ERROR_QTY,
83+
$result->getQuoteMessage()
84+
);
85+
}
86+
5387
/**
5488
* Check product inventory data when quote item quantity declaring
5589
*
@@ -61,7 +95,7 @@ public function __construct(
6195
* @SuppressWarnings(PHPMD.NPathComplexity)
6296
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
6397
*/
64-
public function validate(\Magento\Framework\Event\Observer $observer)
98+
public function validate(Observer $observer)
6599
{
66100
/* @var $quoteItem \Magento\Quote\Model\Quote\Item */
67101
$quoteItem = $observer->getEvent()->getItem();
@@ -81,9 +115,8 @@ public function validate(\Magento\Framework\Event\Observer $observer)
81115
$quoteItem->getProduct()->getId(),
82116
$quoteItem->getProduct()->getStore()->getWebsiteId()
83117
);
84-
/* @var $stockItem \Magento\CatalogInventory\Api\Data\StockItemInterface */
85-
if (!$stockItem instanceof \Magento\CatalogInventory\Api\Data\StockItemInterface) {
86-
throw new \Magento\Framework\Exception\LocalizedException(__('The stock item for Product is not valid.'));
118+
if (!$stockItem instanceof StockItemInterface) {
119+
throw new LocalizedException(__('The stock item for Product is not valid.'));
87120
}
88121

89122
$parentStockItem = false;
@@ -103,19 +136,19 @@ public function validate(\Magento\Framework\Event\Observer $observer)
103136
if (!$stockItem->getIsInStock() || $parentStockItem && !$parentStockItem->getIsInStock()) {
104137
$quoteItem->addErrorInfo(
105138
'cataloginventory',
106-
\Magento\CatalogInventory\Helper\Data::ERROR_QTY,
139+
Data::ERROR_QTY,
107140
__('This product is out of stock.')
108141
);
109142
$quoteItem->getQuote()->addErrorInfo(
110143
'stock',
111144
'cataloginventory',
112-
\Magento\CatalogInventory\Helper\Data::ERROR_QTY,
145+
Data::ERROR_QTY,
113146
__('Some of the products are out of stock.')
114147
);
115148
return;
116149
} else {
117150
// Delete error from item and its quote, if it was set due to item out of stock
118-
$this->_removeErrorsFromQuoteAndItem($quoteItem, \Magento\CatalogInventory\Helper\Data::ERROR_QTY);
151+
$this->_removeErrorsFromQuoteAndItem($quoteItem, Data::ERROR_QTY);
119152
}
120153
}
121154

@@ -134,65 +167,47 @@ public function validate(\Magento\Framework\Event\Observer $observer)
134167
if ($result->getHasError()) {
135168
$quoteItem->addErrorInfo(
136169
'cataloginventory',
137-
\Magento\CatalogInventory\Helper\Data::ERROR_QTY_INCREMENTS,
170+
Data::ERROR_QTY_INCREMENTS,
138171
$result->getMessage()
139172
);
140173

141174
$quoteItem->getQuote()->addErrorInfo(
142175
$result->getQuoteMessageIndex(),
143176
'cataloginventory',
144-
\Magento\CatalogInventory\Helper\Data::ERROR_QTY_INCREMENTS,
177+
Data::ERROR_QTY_INCREMENTS,
145178
$result->getQuoteMessage()
146179
);
147180
} else {
148181
// Delete error from item and its quote, if it was set due to qty problems
149182
$this->_removeErrorsFromQuoteAndItem(
150183
$quoteItem,
151-
\Magento\CatalogInventory\Helper\Data::ERROR_QTY_INCREMENTS
184+
Data::ERROR_QTY_INCREMENTS
152185
);
153186
}
154187
}
188+
// variable to keep track if we have previously encountered an error in one of the options
189+
$removeError = true;
155190

156191
foreach ($options as $option) {
157192
$result = $this->optionInitializer->initialize($option, $quoteItem, $qty);
158193
if ($result->getHasError()) {
159194
$option->setHasError(true);
160-
161-
$quoteItem->addErrorInfo(
162-
'cataloginventory',
163-
\Magento\CatalogInventory\Helper\Data::ERROR_QTY,
164-
$result->getMessage()
165-
);
166-
167-
$quoteItem->getQuote()->addErrorInfo(
168-
$result->getQuoteMessageIndex(),
169-
'cataloginventory',
170-
\Magento\CatalogInventory\Helper\Data::ERROR_QTY,
171-
$result->getQuoteMessage()
172-
);
173-
} else {
174-
// Delete error from item and its quote, if it was set due to qty lack
175-
$this->_removeErrorsFromQuoteAndItem($quoteItem, \Magento\CatalogInventory\Helper\Data::ERROR_QTY);
195+
//Setting this to false, so no error statuses are cleared
196+
$removeError = false;
197+
$this->addErrorInfoToQuote($result, $quoteItem, $removeError);
176198
}
177199
}
200+
if ($removeError) {
201+
$this->_removeErrorsFromQuoteAndItem($quoteItem, Data::ERROR_QTY);
202+
}
178203
} else {
179-
$result = $this->stockItemInitializer->initialize($stockItem, $quoteItem, $qty);
180-
if ($result->getHasError()) {
181-
$quoteItem->addErrorInfo(
182-
'cataloginventory',
183-
\Magento\CatalogInventory\Helper\Data::ERROR_QTY,
184-
$result->getMessage()
185-
);
186-
187-
$quoteItem->getQuote()->addErrorInfo(
188-
$result->getQuoteMessageIndex(),
189-
'cataloginventory',
190-
\Magento\CatalogInventory\Helper\Data::ERROR_QTY,
191-
$result->getQuoteMessage()
192-
);
193-
} else {
194-
// Delete error from item and its quote, if it was set due to qty lack
195-
$this->_removeErrorsFromQuoteAndItem($quoteItem, \Magento\CatalogInventory\Helper\Data::ERROR_QTY);
204+
if ($quoteItem->getParentItem() === null) {
205+
$result = $this->stockItemInitializer->initialize($stockItem, $quoteItem, $qty);
206+
if ($result->getHasError()) {
207+
$this->addErrorInfoToQuote($result, $quoteItem);
208+
} else {
209+
$this->_removeErrorsFromQuoteAndItem($quoteItem, Data::ERROR_QTY);
210+
}
196211
}
197212
}
198213
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public function initialize(
101101
);
102102

103103
$stockItem = $this->getStockItem($option, $quoteItem);
104+
$stockItem->setProductName($option->getProduct()->getName());
104105
$result = $this->stockState->checkQuoteItemQty(
105106
$option->getProduct()->getId(),
106107
$optionQty,

app/code/Magento/CatalogInventory/Test/Unit/Model/Quote/Item/QuantityValidator/Initializer/OptionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ protected function setUp()
106106
'__wakeup',
107107
'unsIsChildItem',
108108
'getItemId',
109+
'setProductName'
109110
];
110111

111112
$this->stockItemMock = $this->getMock(

0 commit comments

Comments
 (0)