9
9
10
10
use Magento \CatalogInventory \Api \StockRegistryInterface ;
11
11
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 ;
12
18
19
+ /**
20
+ * Class QuantityValidator
21
+ */
13
22
class QuantityValidator
14
23
{
15
24
/**
@@ -23,24 +32,25 @@ class QuantityValidator
23
32
protected $ stockItemInitializer ;
24
33
25
34
/**
26
- * @var StockRegistryInterface
35
+ * @var \Magento\CatalogInventory\Api\ StockRegistryInterface
27
36
*/
28
37
protected $ stockRegistry ;
29
38
30
39
/**
31
- * @var StockStateInterface
40
+ * @var \Magento\CatalogInventory\Api\ StockStateInterface
32
41
*/
33
42
protected $ stockState ;
34
43
35
44
/**
36
- * @param QuantityValidator\Initializer\ Option $optionInitializer
37
- * @param QuantityValidator\Initializer\ StockItem $stockItemInitializer
45
+ * @param Option $optionInitializer
46
+ * @param StockItem $stockItemInitializer
38
47
* @param StockRegistryInterface $stockRegistry
39
48
* @param StockStateInterface $stockState
49
+ * @return void
40
50
*/
41
51
public function __construct (
42
- QuantityValidator \ Initializer \ Option $ optionInitializer ,
43
- QuantityValidator \ Initializer \ StockItem $ stockItemInitializer ,
52
+ Option $ optionInitializer ,
53
+ StockItem $ stockItemInitializer ,
44
54
StockRegistryInterface $ stockRegistry ,
45
55
StockStateInterface $ stockState
46
56
) {
@@ -50,6 +60,30 @@ public function __construct(
50
60
$ this ->stockState = $ stockState ;
51
61
}
52
62
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
+
53
87
/**
54
88
* Check product inventory data when quote item quantity declaring
55
89
*
@@ -61,7 +95,7 @@ public function __construct(
61
95
* @SuppressWarnings(PHPMD.NPathComplexity)
62
96
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
63
97
*/
64
- public function validate (\ Magento \ Framework \ Event \ Observer $ observer )
98
+ public function validate (Observer $ observer )
65
99
{
66
100
/* @var $quoteItem \Magento\Quote\Model\Quote\Item */
67
101
$ quoteItem = $ observer ->getEvent ()->getItem ();
@@ -81,9 +115,8 @@ public function validate(\Magento\Framework\Event\Observer $observer)
81
115
$ quoteItem ->getProduct ()->getId (),
82
116
$ quoteItem ->getProduct ()->getStore ()->getWebsiteId ()
83
117
);
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. ' ));
87
120
}
88
121
89
122
$ parentStockItem = false ;
@@ -103,19 +136,19 @@ public function validate(\Magento\Framework\Event\Observer $observer)
103
136
if (!$ stockItem ->getIsInStock () || $ parentStockItem && !$ parentStockItem ->getIsInStock ()) {
104
137
$ quoteItem ->addErrorInfo (
105
138
'cataloginventory ' ,
106
- \ Magento \ CatalogInventory \ Helper \ Data::ERROR_QTY ,
139
+ Data::ERROR_QTY ,
107
140
__ ('This product is out of stock. ' )
108
141
);
109
142
$ quoteItem ->getQuote ()->addErrorInfo (
110
143
'stock ' ,
111
144
'cataloginventory ' ,
112
- \ Magento \ CatalogInventory \ Helper \ Data::ERROR_QTY ,
145
+ Data::ERROR_QTY ,
113
146
__ ('Some of the products are out of stock. ' )
114
147
);
115
148
return ;
116
149
} else {
117
150
// 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 );
119
152
}
120
153
}
121
154
@@ -134,65 +167,47 @@ public function validate(\Magento\Framework\Event\Observer $observer)
134
167
if ($ result ->getHasError ()) {
135
168
$ quoteItem ->addErrorInfo (
136
169
'cataloginventory ' ,
137
- \ Magento \ CatalogInventory \ Helper \ Data::ERROR_QTY_INCREMENTS ,
170
+ Data::ERROR_QTY_INCREMENTS ,
138
171
$ result ->getMessage ()
139
172
);
140
173
141
174
$ quoteItem ->getQuote ()->addErrorInfo (
142
175
$ result ->getQuoteMessageIndex (),
143
176
'cataloginventory ' ,
144
- \ Magento \ CatalogInventory \ Helper \ Data::ERROR_QTY_INCREMENTS ,
177
+ Data::ERROR_QTY_INCREMENTS ,
145
178
$ result ->getQuoteMessage ()
146
179
);
147
180
} else {
148
181
// Delete error from item and its quote, if it was set due to qty problems
149
182
$ this ->_removeErrorsFromQuoteAndItem (
150
183
$ quoteItem ,
151
- \ Magento \ CatalogInventory \ Helper \ Data::ERROR_QTY_INCREMENTS
184
+ Data::ERROR_QTY_INCREMENTS
152
185
);
153
186
}
154
187
}
188
+ // variable to keep track if we have previously encountered an error in one of the options
189
+ $ removeError = true ;
155
190
156
191
foreach ($ options as $ option ) {
157
192
$ result = $ this ->optionInitializer ->initialize ($ option , $ quoteItem , $ qty );
158
193
if ($ result ->getHasError ()) {
159
194
$ 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 );
176
198
}
177
199
}
200
+ if ($ removeError ) {
201
+ $ this ->_removeErrorsFromQuoteAndItem ($ quoteItem , Data::ERROR_QTY );
202
+ }
178
203
} 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
+ }
196
211
}
197
212
}
198
213
}
0 commit comments