Magento remove item from cart programmatically : We sometimes need to remove products(items) from the cart on the basis of sku or id. Here in this tutorial we are going to explain how to remove cart item(product) programmatically.
Magento remove item from cart programmatically
Remove Item(Product) By Sku from Cart
Use the below method to delete the product by sku as –
Magento remove Product from cart programmatically:
<?php public function removeBySku($sku){ $cartHelper = Mage::helper('checkout/cart'); $items = $cartHelper-?>getCart()->getItems(); foreach ($items as $item) { if ($item->getProduct()->getSku() == $sku) { $itemId = $item->getItemId(); $cartHelper->getCart()->removeItem($itemId)->save(); break; } } } ?> |
Call the above method and pass the sku as parameter. If sku matches with the items available in cart it will remove the product.
Remove Item(Product) By Product Id from Cart
Use the below method to delete the product by id as –
Magento remove Product from cart programmatically:
<?php public function removeById($productId){ $cartHelper = Mage::helper('checkout/cart'); $items = $cartHelper-?>getCart()->getItems(); foreach ($items as $item) { if ($item->getProduct()->getId() == $productId) { $itemId = $item->getItemId(); $cartHelper->getCart()->removeItem($itemId)->save(); break; } } } ?> |
Call the above method and pass the product id as parameter. If id matches with the items available in cart it will remove the product.