0

I am looking for the best way to remove inactive products (not available on stock) after user login from cart/mini-cart.

I know that I can set quote lifetime in admin, but I am wondering how to achieve this programmatically.

1

Create events.xml from Module/etc/frontend/ folder and paste it below code.

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_login">
        <observer name="custom_customer_log_login" instance="Theme\VendorName\Observer\UpdateCartAfterCustomerLoginAtObserver" />
    </event>
</config>

UpdateCartAfterCustomerLoginAtObserver.php

<?php

namespace Theme\VendorName\Observer;

use \Magento\Framework\Message\ManagerInterface ;

class UpdateCartAfterCustomerLoginAtObserver implements \Magento\Framework\Event\ObserverInterface{


    /**
     * @var \Magento\Checkout\Model\Cart
     */
    protected $_customerCartSession;

    /**
     * @var \Magento\Catalog\Model\ProductRepository
     */
    protected $_productRepository;

    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $_checkoutSession;

    /**
     * @param \Magento\Checkout\Model\Cart $cart
     * @param \Magento\Checkout\Model\Session $checkoutSession
     */
    public function __construct
    (
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Checkout\Model\Session $checkoutSession
    ){
        $this->_customerCartSession = $cart;
        $this->_checkoutSession = $checkoutSession;
    }


    public function execute(\Magento\Framework\Event\Observer $observer){

        $customer = $observer->getEvent()->getCustomer();
        $cartItems = $this->_checkoutSession->getQuote()->getAllItems();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        foreach ($cartItems as $item) {
            $product = $objectManager->create('Magento\Catalog\Model\Product')->load($item->getProductId());
            if(!$product->getStatus())
                $this->_customerCartSession->removeItem($item->getItemId())->save();
        }

        return true;

    }

}

Like this, you can try your logic.

1

You can use events and observers feature of Magento to achieve this.

Observe the event for customer login and check the stock availability of cart items in the observer function.

If any cart item has got inactive or out of stock, you can delete it from cart programmatically.

You can use below functions to remove item from cart.

public function deleteQuoteItems(){
    $checkoutSession = $this->getCheckoutSession();
    $allItems = $checkoutSession->getQuote()->getAllVisibleItems();//returns all teh items in session
    foreach ($allItems as $item) {
        $itemId = $item->getItemId();//item id of particular item
        $quoteItem=$this->getItemModel()->load($itemId);//load particular item which you want to delete by his item id
        $quoteItem->delete();//deletes the item
    }
}
public function getCheckoutSession(){
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();//instance of object manager 
    $checkoutSession = $objectManager->get('Magento\Checkout\Model\Session');//checkout session
    return $checkoutSession;
}

public function getItemModel(){
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();//instance of object manager
    $itemModel = $objectManager->create('Magento\Quote\Model\Quote\Item');//Quote item model to load quote item
    return $itemModel;
}

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.