How To

How to Remove Disable Product from Cart in Magento 2

Hello, Magento Folks,

Last time we have learned how you can Change MyAccount navigation links title dynamically in Magento 2. Today we are going to learn how you can remove disable products from the cart. So, without further talks, let’s get started.

Now the era of the e-commerce world is up and running, and developers are expanding their coding capabilities to make CMS smarter and flexible. With this view, we are coming up and developing new codes to fulfill the function that any store owner like you require.

There are times when you require to remove disabled products from the cart. These disabled products are of no use to anyone, so it is better to remove them from the cart. To achieve this, we need to create a custom extension and test module to use the below code.

Create a Test Module, in which create these files,

Create file app/code/Vendor/Extension/etc/frontend/event.xml

Use the 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="Vendor\Extension\Observer\UpdateCartAfterCustomerLoginAtObserver"/>
    </event>
</config>

Now, Create file app/code/Vendor/Extension/Observer/

Update cart after customer login at Observer.php

<?php
namespace Vendor\Extension\Observer;
use \Magento\Framework\Message\ManagerInterface ;
class UpdateCartAfterCustomerLoginAtObserver implements \Magento\Framework\Event\ObserverInterface{

    protected $_customerCartSession;
    protected $_productRepository;
    protected $_checkoutSession;
    protected $_productModel;

    public function __construct
    (
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Catalog\Model\Product $productModel
    ){
        $this->_customerCartSession = $cart;
        $this->_checkoutSession = $checkoutSession;
        $this->_productModel = $productModel;
    }
    public function execute(\Magento\Framework\Event\Observer $observer){
        $customer = $observer->getEvent()->getCustomer();
        $cartItems = $this->_checkoutSession->getQuote()->getAllItems();
        foreach ($cartItems as $item) {
            $product = $this->_productModel->load($item->getProductId());
            if(!$product->getStatus())
                $this->_customerCartSession->removeItem($item->getItemId())->save();
        }
        return true;
    }
}

So, that’s it. With little coding knowledge, you can achieve this without any hassle. If you liked this article, then let us know in the comments below. Also, you can share this with your Magento colleagues and friends.

Lastly, if you faced any problems while implementing the code, then you can get in touch with us at our support portal. We will be glad to help you.

Happy Coding!

Click to rate this post!
[Total: 4 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago