32
32
use Magento \Wishlist \Model \ResourceModel \Item \CollectionFactory ;
33
33
use Magento \Wishlist \Model \ResourceModel \Wishlist as ResourceWishlist ;
34
34
use Magento \Wishlist \Model \ResourceModel \Wishlist \Collection ;
35
- use Magento \CatalogInventory \Model \Stock \StockItemRepository ;
36
35
37
36
/**
38
37
* Wishlist model
@@ -148,11 +147,6 @@ class Wishlist extends AbstractModel implements IdentityInterface
148
147
*/
149
148
private $ serializer ;
150
149
151
- /**
152
- * @var StockItemRepository
153
- */
154
- private $ stockItemRepository ;
155
-
156
150
/**
157
151
* @var ScopeConfigInterface
158
152
*/
@@ -178,7 +172,6 @@ class Wishlist extends AbstractModel implements IdentityInterface
178
172
* @param bool $useCurrentWebsite
179
173
* @param array $data
180
174
* @param Json|null $serializer
181
- * @param StockItemRepository|null $stockItemRepository
182
175
* @param ScopeConfigInterface|null $scopeConfig
183
176
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
184
177
*/
@@ -200,7 +193,6 @@ public function __construct(
200
193
$ useCurrentWebsite = true ,
201
194
array $ data = [],
202
195
Json $ serializer = null ,
203
- StockItemRepository $ stockItemRepository = null ,
204
196
ScopeConfigInterface $ scopeConfig = null
205
197
) {
206
198
$ this ->_useCurrentWebsite = $ useCurrentWebsite ;
@@ -216,9 +208,6 @@ public function __construct(
216
208
$ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->get (Json::class);
217
209
parent ::__construct ($ context , $ registry , $ resource , $ resourceCollection , $ data );
218
210
$ this ->productRepository = $ productRepository ;
219
- $ this ->stockItemRepository = $ stockItemRepository ?: ObjectManager::getInstance ()->get (
220
- StockItemRepository::class
221
- );
222
211
$ this ->scopeConfig = $ scopeConfig ?: ObjectManager::getInstance ()->get (ScopeConfigInterface::class);
223
212
}
224
213
@@ -452,18 +441,13 @@ public function addNewItem($product, $buyRequest = null, $forciblySetQty = false
452
441
}
453
442
454
443
try {
444
+ /** @var Product $product */
455
445
$ product = $ this ->productRepository ->getById ($ productId , false , $ storeId );
456
446
} catch (NoSuchEntityException $ e ) {
457
447
throw new LocalizedException (__ ('Cannot specify product. ' ));
458
448
}
459
449
460
- /** @var \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem */
461
- $ stockItem = $ this ->stockItemRepository ->get ($ productId );
462
- $ showOutOfStock = $ this ->scopeConfig ->isSetFlag (
463
- Configuration::XML_PATH_SHOW_OUT_OF_STOCK ,
464
- ScopeInterface::SCOPE_STORE
465
- );
466
- if (!$ stockItem ->getIsInStock () && !$ showOutOfStock ) {
450
+ if ($ this ->isInStock ($ product )) {
467
451
throw new LocalizedException (__ ('Cannot add product without stock to wishlist. ' ));
468
452
}
469
453
@@ -655,6 +639,24 @@ public function isSalable()
655
639
return false ;
656
640
}
657
641
642
+ /**
643
+ * Retrieve if product has stock or config is set for showing out of stock products
644
+ *
645
+ * @param \Magento\Catalog\Api\Data\ProductInterface $product
646
+ * @return bool
647
+ */
648
+ public function isInStock (\Magento \Catalog \Api \Data \ProductInterface $ product )
649
+ {
650
+ /** @var \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem */
651
+ $ stockItem = $ product ->getExtensionAttributes ()->getStockItem ();
652
+ $ showOutOfStock = $ this ->scopeConfig ->isSetFlag (
653
+ Configuration::XML_PATH_SHOW_OUT_OF_STOCK ,
654
+ ScopeInterface::SCOPE_STORE
655
+ );
656
+ $ isInStock = $ stockItem ? $ stockItem ->getIsInStock () : false ;
657
+ return !$ isInStock && !$ showOutOfStock ;
658
+ }
659
+
658
660
/**
659
661
* Check customer is owner this wishlist
660
662
*
0 commit comments