0

I need to modify out of stock error message in cart page in magento2?

In Magento\CatalogInventory\Model\Quote\Item\QuantityValidator.php:

 public function validate(Observer $observer)
    {
        /* @var $quoteItem Item */
        $quoteItem = $observer->getEvent()->getItem();
        if (!$quoteItem ||
            !$quoteItem->getProductId() ||
            !$quoteItem->getQuote()
        ) {
            return;
        }
        $product = $quoteItem->getProduct();echo '  ' . $product->getId();
        $qty = $quoteItem->getQty();

        /* @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */
        $stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
        if (!$stockItem instanceof StockItemInterface) {
            throw new LocalizedException(__('The Product stock item is invalid. Verify the stock item and try again.'));
        }

        if (($options = $quoteItem->getQtyOptions()) && $qty > 0) {
            foreach ($options as $option) {
                $this->optionInitializer->initialize($option, $quoteItem, $qty);
            }
        } else {
            $this->stockItemInitializer->initialize($stockItem, $quoteItem, $qty);
        }

        if ($quoteItem->getQuote()->getIsSuperMode()) {
            return;
        }

        /* @var \Magento\CatalogInventory\Api\Data\StockStatusInterface $stockStatus */
        $stockStatus = $this->stockRegistry->getStockStatus($product->getId(), $product->getStore()->getWebsiteId());

        /* @var \Magento\CatalogInventory\Api\Data\StockStatusInterface $parentStockStatus */
        $parentStockStatus = false;

        /**
         * Check if product in stock. For composite products check base (parent) item stock status
         */
        if ($quoteItem->getParentItem()) {
            $product = $quoteItem->getParentItem()->getProduct();
            $parentStockStatus = $this->stockRegistry->getStockStatus(
                $product->getId(),
                $product->getStore()->getWebsiteId()
            );
        }

        if ($stockStatus) {
            if ($stockStatus->getStockStatus() === Stock::STOCK_OUT_OF_STOCK
                    || $parentStockStatus && $parentStockStatus->getStockStatus() == Stock::STOCK_OUT_OF_STOCK
            ) {
                $quoteItem->addErrorInfo(
                    'cataloginventory',
                    Data::ERROR_QTY,
                    __('This product is out of stock.')
                );
                $quoteItem->getQuote()->addErrorInfo(
                    'stock',
                    'cataloginventory',
                    Data::ERROR_QTY,
                    __('Some of the products are out of stockss.')
                );
                return;
            } else {
                // Delete error from item and its quote, if it was set due to item out of stock
                $this->_removeErrorsFromQuoteAndItem($quoteItem, Data::ERROR_QTY);
            }
        }

        /**
         * Check item for options
         */
        if ($options) {
            $qty = $product->getTypeInstance()->prepareQuoteItemQty($qty, $product);
            $quoteItem->setData('qty', $qty);
            if ($stockStatus) {
                $this->checkOptionsQtyIncrements($quoteItem, $options);
            }

            // variable to keep track if we have previously encountered an error in one of the options
            $removeError = true;
            foreach ($options as $option) {
                $result = $option->getStockStateResult();
                if ($result->getHasError()) {
                    $option->setHasError(true);
                    //Setting this to false, so no error statuses are cleared
                    $removeError = false;
                    $this->addErrorInfoToQuote($result, $quoteItem, $removeError);
                }
            }
            if ($removeError) {
                $this->_removeErrorsFromQuoteAndItem($quoteItem, Data::ERROR_QTY);
            }
        } else {
            if ($quoteItem->getParentItem() === null) {
                $result = $quoteItem->getStockStateResult();
                if ($result->getHasError()) {
                    $this->addErrorInfoToQuote($result, $quoteItem);
                } else {
                    $this->_removeErrorsFromQuoteAndItem($quoteItem, Data::ERROR_QTY);
                }
            }
        }
    }

Why way is better and correct one to change stock error message.

Thanks.

0

You could rewrite the class. Something like this. Depends on the level of change required though.

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\Quote\Item\QuantityValidator" type="Xigen\Stock\Rewrite\Magento\CatalogInventory\Model\Quote\Item\QuantityValidator"/>
</config>

QuantityValidator.php

namespace Xigen\Stock\Rewrite\Magento\CatalogInventory\Model\Quote\Item;

class QuantityValidator extends \Magento\CatalogInventory\Model\Quote\Item\QuantityValidator
{
    public function validate(Observer $observer)
    {
        // changes
    }
}
0

change it in i18n/en_US.csv

"This product is out of stock." to your modified errorMessage

if you want to add message on condition base please make after plugin:

in di.xml

        <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
            <type name="Magento\CatalogInventory\Model\Quote\Item\QuantityValidator">
                <plugin name="Vendor_Module_Plugin" type="Vendor\Module\Plugin\ValidatorPlugin" sortOrder="10" disabled="false"  />
            </type>
        </config>

in Vendor\Module\Plugin\ValidatorPlugin.php file

                <?php

                namespace Vendor\Module\Plugin;

                class ValidatorPlugin
                {

                    public function afterValidate(\Magento\CatalogInventory\Model\Quote\Item\QuantityValidator $subject, \Magento\Framework\Event\Observer $observer)
                    {

                        /* @var $quoteItem Item */
                        $quoteItem = $observer->getEvent()->getItem();
                        if (!$quoteItem ||
                            !$quoteItem->getProductId() ||
                            !$quoteItem->getQuote()
                        ) {
                            return;
                        }
                        $product = $quoteItem->getProduct();
                        $qty = $quoteItem->getQty();

                        /* @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */
                        $stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
                        if (!$stockItem instanceof StockItemInterface) {
                            throw new LocalizedException(__('The Product stock item is invalid. Verify the stock item and try again.'));
                        }

                        if (($options = $quoteItem->getQtyOptions()) && $qty > 0) {
                            foreach ($options as $option) {
                                $this->optionInitializer->initialize($option, $quoteItem, $qty);
                            }
                        } else {
                            $this->stockItemInitializer->initialize($stockItem, $quoteItem, $qty);
                        }

                        if ($quoteItem->getQuote()->getIsSuperMode()) {
                            return;
                        }

                        /* @var \Magento\CatalogInventory\Api\Data\StockStatusInterface $stockStatus */
                        $stockStatus = $this->stockRegistry->getStockStatus($product->getId(), $product->getStore()->getWebsiteId());

                        /* @var \Magento\CatalogInventory\Api\Data\StockStatusInterface $parentStockStatus */
                        $parentStockStatus = false;

                        /**
                         * Check if product in stock. For composite products check base (parent) item stock status
                         */
                        if ($quoteItem->getParentItem()) {
                            $product = $quoteItem->getParentItem()->getProduct();
                            $parentStockStatus = $this->stockRegistry->getStockStatus(
                                $product->getId(),
                                $product->getStore()->getWebsiteId()
                            );
                        }

                        if ($stockStatus) {
                            if ($stockStatus->getStockStatus() === Stock::STOCK_OUT_OF_STOCK
                                    || $parentStockStatus && $parentStockStatus->getStockStatus() == Stock::STOCK_OUT_OF_STOCK
                            ) {
                                $quoteItem->addErrorInfo(
                                    'cataloginventory',
                                    Data::ERROR_QTY,
                                    __('This product is out of stock.')
                                );

                                //make custom condition here

                                $quoteItem->getQuote()->addErrorInfo(
                                    'stock',
                                    'cataloginventory',
                                    Data::ERROR_QTY,
                                    __('Some of the products are out of stock.')
                                );
                                return;
                            } else {
                                // Delete error from item and its quote, if it was set due to item out of stock
                                $this->_removeErrorsFromQuoteAndItem($quoteItem, Data::ERROR_QTY);
                            }
                        }

                        /**
                         * Check item for options
                         */
                        if ($options) {
                            $qty = $product->getTypeInstance()->prepareQuoteItemQty($qty, $product);
                            $quoteItem->setData('qty', $qty);
                            if ($stockStatus) {
                                $this->checkOptionsQtyIncrements($quoteItem, $options);
                            }

                            // variable to keep track if we have previously encountered an error in one of the options
                            $removeError = true;
                            foreach ($options as $option) {
                                $result = $option->getStockStateResult();
                                if ($result->getHasError()) {
                                    $option->setHasError(true);
                                    //Setting this to false, so no error statuses are cleared
                                    $removeError = false;
                                    $this->addErrorInfoToQuote($result, $quoteItem, $removeError);
                                }
                            }
                            if ($removeError) {
                                $this->_removeErrorsFromQuoteAndItem($quoteItem, Data::ERROR_QTY);
                            }
                        } else {
                            if ($quoteItem->getParentItem() === null) {
                                $result = $quoteItem->getStockStateResult();
                                if ($result->getHasError()) {
                                    $this->addErrorInfoToQuote($result, $quoteItem);
                                } else {
                                    $this->_removeErrorsFromQuoteAndItem($quoteItem, Data::ERROR_QTY);
                                }
                            }
                        }
                    }

                    }
  • I want to add some condition on it. – Mahi M May 23 '19 at 12:25
  • then make after plugin for it @ManoM – Rutvee Sojitra May 23 '19 at 12:26
  • @ManoM i have updated my answer check – Rutvee Sojitra May 23 '19 at 12:38
  • Add "Parent product is out of stock" and "child product is out of stock" based upon stock – Mahi M May 23 '19 at 12:43
  • change if ($stockStatus->getStockStatus() === Stock::STOCK_OUT_OF_STOCK || $parentStockStatus && $parentStockStatus->getStockStatus() == Stock::STOCK_OUT_OF_STOCK ) in above plugin – Rutvee Sojitra May 23 '19 at 12:45

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.