0

scenario- when the user doing his shopping he added some products to the basket, that are shown in the minicart by default. But when the click on mini cart (or) when the user refresh the page i need to remove of stock items from minicart and display error msg "xxx,yyy products are removed due to out of stock" inside minicart . is this possible? if it is can anyone pls explain indetail. thanks.

1 Answer 1

0

You can use the sales_quote_save_before event to remove the out of stocked item from the quote place your logic in commented section

events.xml

<event name="sales_quote_save_before">
    <observer name="remove_out_of_stock_item" instance="Vendor\Module\Observer\Remove"/>
</event>

Remove.php

public function execute(Observer $observer)
{

        /** @var $quote \Magento\Quote\Model\Quote */
        $quote = $observer->getEvent()->getQuote();
        $result = $quote->getAllItems();
        foreach ($result as $item) {
          // get product from item and check stock status if out of stock then delete the item else proceed
               $quote->deleteItem($item);

                }
        $quote->setTotalsCollectedFlag(false)->collectTotals();

    return $this;

}
2
  • thanku @Ghulam.M. When that event is triggered
    – Mani
    Commented Mar 14, 2022 at 10:02
  • and how can i add error message in the minicart
    – Mani
    Commented Mar 14, 2022 at 10:22

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.