Programmatically remove product from cart (by ID)

Submitted by deeperdata - 3 years ago

I used this outside of Magento but it can be used in PHTML files as well I believe.

<?php
$id = '100'; // replace product id with your id
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach($items as $item):
if($item->getProduct()->getId() == $id):
$itemId = $item->getItemId();
$cartHelper->getCart()->removeItem($itemId)->save();
break;
endif;
endforeach;
?>
affiliate_link
Learn PHP
comments powered by Disqus
Proudly hosted on Digital Ocean