Skip to content

MSI compatibility while being backward compatible #1739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Helper/Entity/ProductHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\CatalogInventory\Helper\Stock;
use Magento\CatalogInventory\Model\ResourceModel\Stock\Status as StockStatus;
use Magento\Customer\Api\GroupExcludedWebsiteRepositoryInterface;
use Magento\Customer\Model\ResourceModel\Group\Collection as GroupCollection;
use Magento\Directory\Model\Currency as CurrencyHelper;
Expand Down Expand Up @@ -80,7 +80,6 @@ public function __construct(
protected StoreManagerInterface $storeManager,
protected ManagerInterface $eventManager,
protected Visibility $visibility,
protected Stock $stockHelper,
protected CurrencyHelper $currencyManager,
protected Type $productType,
protected CollectionFactory $productCollectionFactory,
Expand All @@ -90,6 +89,7 @@ public function __construct(
protected ReplicaManagerInterface $replicaManager,
protected ProductInterfaceFactory $productFactory,
protected ProductRecordBuilder $productRecordBuilder,
protected StockStatus $stockStatus,
)
{
parent::__construct($indexNameFetcher);
Expand Down Expand Up @@ -239,9 +239,10 @@ public function getProductCollectionQuery(
*/
protected function addStockFilter($products, $storeId): void
{
if ($this->configHelper->getShowOutOfStock($storeId) === false) {
$this->stockHelper->addInStockFilterToCollection($products);
}
$this->stockStatus->addStockDataToCollection(
$products,
$this->configHelper->getShowOutOfStock($storeId) === false
);
}

/**
Expand Down
15 changes: 5 additions & 10 deletions Service/Product/RecordBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ protected function addImageData(array $customData, Product $product, $additional
public function addInStock($defaultData, $customData, Product $product)
{
if (isset($defaultData['in_stock']) === false) {
$stockItem = $this->stockRegistry->getStockItem($product->getId());
$customData['in_stock'] = $product->isSaleable() && $stockItem->getIsInStock();
$customData['in_stock'] = $this->productIsInStock($product, $product->getStoreId());
}

return $customData;
Expand All @@ -288,10 +287,8 @@ protected function addStockQty($defaultData, $customData, $additionalAttributes,
&& $this->isAttributeEnabled($additionalAttributes, 'stock_qty')) {
$customData['stock_qty'] = 0;

$stockItem = $this->stockRegistry->getStockItem($product->getId());
if ($stockItem) {
$customData['stock_qty'] = (int)$stockItem->getQty();
}
$stockStatus = $this->stockRegistry->getStockStatus($product->getId(), $product->getStore()->getWebsiteId());
$customData['stock_qty'] = (int)$stockStatus->getQty();
}

return $customData;
Expand Down Expand Up @@ -804,14 +801,12 @@ protected function getSanitizedArrayValues(array $values, string $attributeName)
* Returns is product in stock
*
* @param Product $product
* @param int $storeId
* @param int $storeId (deprecated)
*
* @return bool
*/
public function productIsInStock($product, $storeId): bool
{
$stockItem = $this->stockRegistry->getStockItem($product->getId());

return $product->isSaleable() && $stockItem->getIsInStock();
return (bool)$product->isSalable();
}
}