12
12
use Magento \Catalog \Api \ProductRepositoryInterface ;
13
13
use Magento \Catalog \Model \Product ;
14
14
use Magento \Catalog \Model \ProductFactory ;
15
+ use Magento \CatalogInventory \Api \Data \StockItemInterface ;
16
+ use Magento \CatalogInventory \Api \StockRegistryInterface ;
15
17
use Magento \CatalogInventory \Model \Configuration ;
16
18
use Magento \Framework \App \Config \ScopeConfigInterface ;
17
19
use Magento \Framework \App \ObjectManager ;
@@ -152,6 +154,11 @@ class Wishlist extends AbstractModel implements IdentityInterface
152
154
*/
153
155
private $ scopeConfig ;
154
156
157
+ /**
158
+ * @var StockRegistryInterface|null
159
+ */
160
+ private $ stockRegistry ;
161
+
155
162
/**
156
163
* Constructor
157
164
*
@@ -172,6 +179,7 @@ class Wishlist extends AbstractModel implements IdentityInterface
172
179
* @param bool $useCurrentWebsite
173
180
* @param array $data
174
181
* @param Json|null $serializer
182
+ * @param StockRegistryInterface|null $stockRegistry
175
183
* @param ScopeConfigInterface|null $scopeConfig
176
184
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
177
185
*/
@@ -193,6 +201,7 @@ public function __construct(
193
201
$ useCurrentWebsite = true ,
194
202
array $ data = [],
195
203
Json $ serializer = null ,
204
+ StockRegistryInterface $ stockRegistry = null ,
196
205
ScopeConfigInterface $ scopeConfig = null
197
206
) {
198
207
$ this ->_useCurrentWebsite = $ useCurrentWebsite ;
@@ -209,6 +218,7 @@ public function __construct(
209
218
parent ::__construct ($ context , $ registry , $ resource , $ resourceCollection , $ data );
210
219
$ this ->productRepository = $ productRepository ;
211
220
$ this ->scopeConfig = $ scopeConfig ?: ObjectManager::getInstance ()->get (ScopeConfigInterface::class);
221
+ $ this ->stockRegistry = $ stockRegistry ?: ObjectManager::getInstance ()->get (StockRegistryInterface::class);
212
222
}
213
223
214
224
/**
@@ -447,7 +457,7 @@ public function addNewItem($product, $buyRequest = null, $forciblySetQty = false
447
457
throw new LocalizedException (__ ('Cannot specify product. ' ));
448
458
}
449
459
450
- if ($ this ->isInStock ($ product )) {
460
+ if ($ this ->isInStock ($ productId )) {
451
461
throw new LocalizedException (__ ('Cannot add product without stock to wishlist. ' ));
452
462
}
453
463
@@ -642,13 +652,13 @@ public function isSalable()
642
652
/**
643
653
* Retrieve if product has stock or config is set for showing out of stock products
644
654
*
645
- * @param \Magento\Catalog\Api\Data\ProductInterface $product
655
+ * @param int $productId
646
656
* @return bool
647
657
*/
648
- public function isInStock (\ Magento \ Catalog \ Api \ Data \ ProductInterface $ product )
658
+ private function isInStock ($ productId )
649
659
{
650
- /** @var \Magento\CatalogInventory\Api\Data\ StockItemInterface $stockItem */
651
- $ stockItem = $ product -> getExtensionAttributes () ->getStockItem ();
660
+ /** @var StockItemInterface $stockItem */
661
+ $ stockItem = $ this -> stockRegistry ->getStockItem ($ productId );
652
662
$ showOutOfStock = $ this ->scopeConfig ->isSetFlag (
653
663
Configuration::XML_PATH_SHOW_OUT_OF_STOCK ,
654
664
ScopeInterface::SCOPE_STORE
0 commit comments