3

I have facing issue in update admin product stock status Out of Stock to In Stock in Magento 2.3.1

I have updated product quantity 0 and save the product that time automatically updated stock status In Stock to Out of Stock. But after adding quantity more than 0 and save the product, but stock status can't getting updated Out of Stock to In Stock.

Any help will appreciate. Thanks.

  • check your settings in advance inventory – fmsthird Apr 23 at 5:16
  • Yes I have checked. – Kirti Nariya Apr 24 at 5:10
  • is it set to automatic? – fmsthird Apr 24 at 5:13
  • Yes already checked but not working. – Kirti Nariya Apr 24 at 13:22
  • 1
    Disable the backorders and it works fine – Jaisa May 7 at 9:19
3

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;
    }
}
0

This is the result of your steps: Source class "\Vendor\UpdateStockStatus\Model\Stock\StockItemInterface" for "Vendor\UpdateStockStatus\Model\Stock\StockItemRepository" generation does not exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.