Skip to content

Commit 9a1b781

Browse files
committed
Refactor the way for getting the stock, using the current product instead of instantiating a new stock item
1 parent 71d1ebc commit 9a1b781

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

app/code/Magento/Wishlist/Model/Wishlist.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory;
3333
use Magento\Wishlist\Model\ResourceModel\Wishlist as ResourceWishlist;
3434
use Magento\Wishlist\Model\ResourceModel\Wishlist\Collection;
35-
use Magento\CatalogInventory\Model\Stock\StockItemRepository;
3635

3736
/**
3837
* Wishlist model
@@ -148,11 +147,6 @@ class Wishlist extends AbstractModel implements IdentityInterface
148147
*/
149148
private $serializer;
150149

151-
/**
152-
* @var StockItemRepository
153-
*/
154-
private $stockItemRepository;
155-
156150
/**
157151
* @var ScopeConfigInterface
158152
*/
@@ -178,7 +172,6 @@ class Wishlist extends AbstractModel implements IdentityInterface
178172
* @param bool $useCurrentWebsite
179173
* @param array $data
180174
* @param Json|null $serializer
181-
* @param StockItemRepository|null $stockItemRepository
182175
* @param ScopeConfigInterface|null $scopeConfig
183176
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
184177
*/
@@ -200,7 +193,6 @@ public function __construct(
200193
$useCurrentWebsite = true,
201194
array $data = [],
202195
Json $serializer = null,
203-
StockItemRepository $stockItemRepository = null,
204196
ScopeConfigInterface $scopeConfig = null
205197
) {
206198
$this->_useCurrentWebsite = $useCurrentWebsite;
@@ -216,9 +208,6 @@ public function __construct(
216208
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
217209
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
218210
$this->productRepository = $productRepository;
219-
$this->stockItemRepository = $stockItemRepository ?: ObjectManager::getInstance()->get(
220-
StockItemRepository::class
221-
);
222211
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
223212
}
224213

@@ -452,18 +441,13 @@ public function addNewItem($product, $buyRequest = null, $forciblySetQty = false
452441
}
453442

454443
try {
444+
/** @var Product $product */
455445
$product = $this->productRepository->getById($productId, false, $storeId);
456446
} catch (NoSuchEntityException $e) {
457447
throw new LocalizedException(__('Cannot specify product.'));
458448
}
459449

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)) {
467451
throw new LocalizedException(__('Cannot add product without stock to wishlist.'));
468452
}
469453

@@ -655,6 +639,24 @@ public function isSalable()
655639
return false;
656640
}
657641

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+
658660
/**
659661
* Check customer is owner this wishlist
660662
*

0 commit comments

Comments
 (0)