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
Leave a comment
Welcome to my Blog
Certifications
Categories
- Apache (2)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (136)
- Magento admin (58)
- Magento Certification (2)
- Magento error (13)
- Magento frontend (68)
- Magento Interview (5)
- Magento2 (5)
- Mobile (1)
- MySQL (7)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (1)
- Wordpress (3)
- XML (2)
Useful Links
Tag Cloud
500 internal server error admin answers attribute bug category checkbox checkout cookie customer difference domain name EAV error event extension interview invoice jquery linux magento magento2 magento admin magento error magento interview questions magento orm mysql observer order pinterest product products questions redirect register remove script session simplexml to array state status study guide tax url wordpress
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:
$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..