class Inchoo_EmptyCart_Model_Observer
Now what happened here you might ask. Since this is a helper extension, I wanted to keep template file inside extension directory, as it’s simple it can get. And both Model and Block provided me with that. Only thing missing in this part is content.phtml, which consists of these few lines:
At this moment you have injected link “Empty cart” into Magento’s Cart page. Finally the extensions logic is here (IndexController.php):
<?php
class Inchoo_EmptyCart_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
//Get cart helper
$cartHelper = Mage::helper('checkout/cart');
//Get all items from cart
$items = $cartHelper->getCart()->getItems();
//Loop through all of cart items
foreach ($items as $item) {
$itemId = $item->getItemId();
//Remove items, one by one
$cartHelper->getCart()->removeItem($itemId)->save();
}
//Redirect back to cart or wherever you wish
$this->_redirect('checkout/cart');
}
}
And that’s pretty much it. In action it looks like this:
Best Answer
You may also like to do that by creating an extension that consists of Block, Controller, XML, Model and Template with very little logic inside.
So let’s start with XML (config.xml):
Besides there’s a hoot to event here (for link injection into cart), it’s pretty much basic stuff.
Now on to bit more fun stuff. Model (Observer.php):
And Block (Injection.php):
class Inchoo_EmptyCart_Model_Observer Now what happened here you might ask. Since this is a helper extension, I wanted to keep template file inside extension directory, as it’s simple it can get. And both Model and Block provided me with that. Only thing missing in this part is content.phtml, which consists of these few lines:
At this moment you have injected link “Empty cart” into Magento’s Cart page. Finally the extensions logic is here (IndexController.php):
And that’s pretty much it. In action it looks like this:
source link