Magento 2: Product stock status is not automatically updated from Out of Stock to In Stock
I have fixed the issue of product stock status update from Out of Stock to In Stock.
Fixed the issue to override module-catalog-inventory/Model/Stock/StockItemRepository.php
file.
Follow below steps to create custom module for override StockItemRepository.php
file and updated code.
File path: magento/app/code/Vendor/UpdateStockStatus/registration.php
<?php
use \Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_UpdateStockStatus', __DIR__);
File path: magento/app/code/Vendor/UpdateStockStatus/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_UpdateStockStatus" >
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
File path: magento/app/code/Vendor/UpdateStockStatus/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\CatalogInventory\Model\Stock\StockItemRepository" type="Vendor\UpdateStockStatus\Model\Stock\StockItemRepository"/>
</config>
File path: magento/app/code/Vendor/UpdateStockStatus/Model/Stock/StockItemRepository.php
<?php
namespace Vendor\UpdateStockStatus\Model\Stock;
use Magento\Catalog\Model\ProductFactory;
use Magento\CatalogInventory\Api\Data\StockItemCollectionInterfaceFactory;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
use Magento\CatalogInventory\Model\Indexer\Stock\Processor;
use Magento\CatalogInventory\Model\ResourceModel\Stock\Item as StockItemResource;
use Magento\CatalogInventory\Model\Spi\StockStateProviderInterface;
use Magento\CatalogInventory\Model\StockRegistryStorage;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DB\MapperFactory;
use Magento\Framework\DB\QueryBuilderFactory;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
class StockItemRepository extends \Magento\CatalogInventory\Model\Stock\StockItemRepository
{
public function __construct(
StockConfigurationInterface $stockConfiguration,
StockStateProviderInterface $stockStateProvider,
StockItemResource $resource,
StockItemInterfaceFactory $stockItemFactory,
StockItemCollectionInterfaceFactory $stockItemCollectionFactory,
ProductFactory $productFactory,
QueryBuilderFactory $queryBuilderFactory,
MapperFactory $mapperFactory,
TimezoneInterface $localeDate,
Processor $indexProcessor,
DateTime $dateTime,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory = null
) {
parent::__construct(
$stockConfiguration,
$stockStateProvider,
$resource,
$stockItemFactory,
$stockItemCollectionFactory,
$productFactory,
$queryBuilderFactory,
$mapperFactory,
$localeDate,
$indexProcessor,
$dateTime,
$productCollectionFactory = null
);
}
/**
* @inheritdoc
*/
public function save(\Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem)
{
try {
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->productCollectionFactory->create()
->setFlag('has_stock_status_filter')
->addIdFilter($stockItem->getProductId())
->addFieldToSelect('type_id')
->getFirstItem();
if (!$product->getId()) {
return $stockItem;
}
$typeId = $product->getTypeId() ?: $product->getTypeInstance()->getTypeId();
$isQty = $this->stockConfiguration->isQty($typeId);
if ($isQty) {
$isInStock = $this->stockStateProvider->verifyStock($stockItem);
if ($stockItem->getManageStock()) {
$isInStockFlag = $isInStock ? true : false;
$stockItem->setIsInStock($isInStockFlag)->setStockStatusChangedAutomaticallyFlag(true);
}
// if qty is below notify qty, update the low stock date to today date otherwise set null
$stockItem->setLowStockDate(null);
if ($this->stockStateProvider->verifyNotification($stockItem)) {
$stockItem->setLowStockDate($this->dateTime->gmtDate());
}
$stockItem->setStockStatusChangedAuto(0);
if ($stockItem->hasStockStatusChangedAutomaticallyFlag()) {
$stockItem->setStockStatusChangedAuto((int)$stockItem->getStockStatusChangedAutomaticallyFlag());
}
} else {
$stockItem->setQty(0);
}
$stockItem->setWebsiteId($stockItem->getWebsiteId());
$stockItem->setStockId($stockItem->getStockId());
$this->resource->save($stockItem);
} catch (\Exception $exception) {
throw new CouldNotSaveException(__('The stock item was unable to be saved. Please try again.'), $exception);
}
return $stockItem;
}
}