I created a module to add available qty to cart when customer request more than available qty. below is my code set.


<?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="E25media\MagentoCommunity\Plugin\Quote" sortOrder="1"/>


namespace E25media\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,
) {
    if ($request === null) {
        $request = 1;
    if (is_numeric($request)) {
        $request = $this->objectFactory->create(['qty' => $request]);

    $messageCollection = $this->messageManager->getMessages(true);
    $messageCollection->getLastAddedMessage()->getText();   //Getting an error in this line.          
    if ($product->getId() && $request) {
        $stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
        if ($stockItem->getQty() < $request->getQty()) {
            $remainingQty = $request->getQty() - $stockItem->getQty();
            $this->messageManager->addSuccess(__('%1 item not available', $remainingQty));

    return [$product, $request];

as per requirement product is adding to the cart. I want to concatenate the message and add some more part to the message to show in the product profile page.


above line my code is breaking. Any help to complete my task higly apreciated

  • what is the error message? – fmsthird May 9 at 8:22
  • no error messages, code break in that line.if i assign that line to a variable like this $message = $messageCollection->getLastAddedMessage()->getText(); , i am getting an error undefined variable $message. – Mujahidh May 9 at 8:29
  • can you try answer below – fmsthird May 9 at 8:49

Remove the true parameter from $this->messageManager->getMessages(true);
Sending this parameter retrieves the messages from the session and clears them.
So the next time you call messageManager->getMessages() you get an empty list of messages.

  • thank you for your answer,I am still getting a break on the same line,if i remove the line code is running. – Mujahidh May 9 at 10:44

Try adding getMessages() method:

  • i am adding that getMessages() on upline like this $messageCollection = $this->messageManager->getMessages(true); – Mujahidh May 9 at 9:13
  • check marius answer. I think he is right – fmsthird May 9 at 9:18

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.