Aug 22, 2014
kalpesh

Magento auto remove out of stock items from shopping cart

Magento automatically remove product items from shopping cart page which are out of stock. You may need this feature when the situation aries where product goes out of stock and that particular product is already there in other customer’s cart who have not yet checked it out. With this script Magento will auto-check if all the items in the cart are available and in-stock before proceeding for checkout page.

In config.xml file:

1
2
3
4
5
6
7
8
9
10
11
<events>
  <controller_action_predispatch_checkout_cart_index>
    <observers>
      <namespace_module_autoremove_outofstock>
        <type>singleton</type>
        <class>namespace_module/observer</class>
        <method>autoRemoveOutOfStockItems</method>
      </namespace_module_autoremove_outofstock>
    </observers>
  </controller_action_predispatch_checkout_cart_index>
</events>

In Observer.php file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public function autoRemoveOutOfStockItems($observer) {
    $quote = Mage::getModel('checkout/session')->getQuote();
    $cartItems = $quote->getAllItems();
    foreach ($cartItems as $item)
    {
        //$productType = $item->getProduct()->getTypeId();
        //if($productType!='configurable') {
        $productId = $item->getProductId();
        $product = Mage::getModel('catalog/product')->load($productId);
        $stockItem = $product->getStockItem();
        if(!$stockItem->getIsInStock())
        {
                Mage::helper('checkout/cart')->getCart()->removeItem($item->getId())->save();
        }
        //}
    }

}

3 Comments

  • Hello Kalpesh,

    Thank you so much for you generosity in posting code. I am a contractor, enhancing an existing website. I am pretty new to Magento. Which Observer.php do I put the code into listed aabove.. there are quite a few.

    I have one direct question and one indirect question. I am extending the website allowing the site owners to become an affiliate and using a service to deliver products electronically. If, during an oredr (which may contain other products), the request to the service fails (database problem on their end, server error, or just site down for whatever reason, I want to roll back the order. Can you give me an idea of where I need to put the logic? Do you know of any extensions that might help with this?

    Thanks
    Mark

    • Hi Mark, you can place it in your custom module observer file. You can check for custom local modules at app/code/local/ and probably put the code in one of the module you or other developer created for extending/adding some functionality to the website.

      If something goes wrong during placing an order, Magento will not commit the transaction at all. Magento roll-backs the entire transaction if there is error in the order process. If this is something you want to do it custom, you can do something like this:

      1
      2
      3
      4
      5
      6
      7
      8
      
      $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
      try {
          $connection->beginTransaction();
          // Do all the database write operations like saving in different tables
          $connection->commit();
      } catch (Exception $e) {
          $connection->rollback();
      }
  • Thx bdy for sharing this..

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications