Magento 2 Module


How to remove out of stock items from the shopping cart in Magento 2


Hide/Show description

Hello, My name is Cuong, I'm a Magento Developer, Welcome back to my Magento 2 tutorial video series. #Magento2Tutorial #Magento2Training #Magento2 Today, I am going to show everybody the best practice, How to remove out of stock items from the shopping cart in Magento 2. By default, the out of stock items won't be auto removed from the cart of a customer, the customer must remove it manually before placing an order. You can see this by going to the checkout cart page. There is a product out of stock in my cart and I can not place an order. We have the scenario: We will create a new feature that allows us to remove out of stock items from the shopping cart of a customer automatically, after that customer logs in success. So what will we do in this practice? 1. We will create a new module called PHPCuong_OutOfStockItem 2. We will use the event named customer_login to remove all out of stock items from the cart of a customer after that customer logged in success. Okie, let's go. Let's complete this practice you need to follow steps by step: Step 1: Declaring the new module called PHPCuong_OutOfStockItem - Create the namespace PHPCuong in the path app\code - Create the module named OutOfStockItem in the path app\code\PHPCuong - Create the file named registration.php in the path app\code\PHPCuong\OutOfStockItem - Create the file named module.xml in the path app\code\PHPCuong\OutOfStockItem\etc End of step #1, I have been completed the codes to create the new module called PHPCuong_OutOfStockItem. Step 2: Subscribing to the event named customer_login You can see this event in the module named Magento_Customer. - Create the new file named events.xml in the path app\code\PHPCuong\OutOfStockItem\etc\frontend - Create the new file named RemoveOutOfStockItem.php in the path app\code\PHPCuong\OutOfStockItem\Observer The table name saves the items in the cart is "quote_item" This is the cart id of the customer id: 1 Move that codes into the try and catch so we can catch the errors Step 3: Test and See the results 1. Run the following command lines: php bin/magento setup:upgrade --keep-generated You can see the new module is saved in the table named setup_module php bin/magento setup:di:compile this command line will take much time, it depends on your server speed. php bin/magento cache:flush 2. Go to the storefront. The customer tries to login Yeah, It works perfectly. For now, I will try to test with another product. Logout and login again. The out of stock item has successfully been removed. Thanks for watching the guide. If you have any questions about this practice, please feel free to leave a comment below. Don't forget to like, comment, share my videos and subscribe to my channel for getting the latest lessons. Please do not hesitate to contact me, if you need me to join your Magento project. My rate is $25/hour in Magento 1 and $30/hour in Magento 2. Get the source codes of this lesson in the Github https://github.com/php-cuong/magento2-out-of-stock-items