1

As asked in the question, Is there any possibility to add available qty(in stock) to the cart when customer requesting more than available qty.

How magento behave with this situation?

ex :

I have 5 qty in product A, Customer requesting 10. I want to add the available 5 to the cart and show a message to customer.

2
  • You can apply check using jquery, can you please share your code?
    – ASQ
    Commented May 7, 2019 at 6:09
  • i did not try in any code level.very beginning i am asking this question.
    – Mujahidh
    Commented May 7, 2019 at 6:10

1 Answer 1

2

Try using plugin:

app/code/SR/MagentoCommunity/etc/frontend/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">
    <type name="Magento\Quote\Model\Quote">
        <plugin name="sr_check_stock" type="SR\MagentoCommunity\Plugin\Quote" sortOrder="1"/>
    </type>
</config>

app/code/SR/MagentoCommunity/Plugin/Quote.php

<?php
namespace SR\MagentoCommunity\Plugin;

use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\DataObject\Factory as DataObjectFactory;

class Quote
{
    /**
     * @var StockRegistryInterface
     */
    private $stockRegistry;

    /**
     * @var ManagerInterface
     */
    private $messageManager;

    /**
     * @var DataObjectFactory
     */
    private $objectFactory;

    /**
     * Quote constructor.
     *
     * @param StockRegistryInterface $stockRegistry
     * @param ManagerInterface $messageManager
     * @param DataObjectFactory $objectFactory
     */
    public function __construct(
        StockRegistryInterface $stockRegistry,
        ManagerInterface $messageManager,
        DataObjectFactory $objectFactory
    ) {
        $this->stockRegistry = $stockRegistry;
        $this->messageManager = $messageManager;
        $this->objectFactory = $objectFactory;
    }

    public function beforeAddProduct(
        \Magento\Quote\Model\Quote $subject,
        \Magento\Catalog\Model\Product $product,
        $request
    ) {
        if ($request === null) {
            $request = 1;
        }
        if (is_numeric($request)) {
            $request = $this->objectFactory->create(['qty' => $request]);
        }

        if ($product->getId() && $request) {
            $stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
            if ($stockItem->getQty() < $request->getQty()) {
                $remainingQty = $request->getQty() - $stockItem->getQty();
                $request->setQty($stockItem->getQty());
                $this->messageManager->addErrorMessage(__('%1 item not available', $remainingQty));
            }
        }

        return [$product, $request];
    }
}
9
  • thank you, was these files do the work or want to completely add the plugin files
    – Mujahidh
    Commented May 7, 2019 at 6:57
  • You need to adjust with your module.
    – Sohel Rana
    Commented May 7, 2019 at 7:05
  • do i need add any observer here or just with the basic module setup and add these files to relevent location is enough?
    – Mujahidh
    Commented May 7, 2019 at 8:21
  • just with the basic module setup and add these files to relevent location is enough?
    – Sohel Rana
    Commented May 7, 2019 at 8:24
  • thank you very much
    – Mujahidh
    Commented May 7, 2019 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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