Question

I am currently developing Magento 1.9 (yes I know it's outdated) and they have requested remove from cart buttons on the category page (list.phtml), however, I cannot seem to find a way to add the button.

I have successfully added the button to the page, but can't seem to get the actual like to be correct with the correct data, I believe this to be because I cannot match the quote id to the product id correctly, however it also seems that this gives a much shorter form key than what is normally used by Magento.

$productid = $_product->getId();
<a href="<?php echo $this->getUrl('checkout/cart/delete', array('id' => $productid, Mage_Core_Model_Url::FORM_KEY => $this->_getSingletonModel('core/session')->getFormKey())); ?>">X</a>

Any help would be much appreciated

Was it helpful?

Solution

This could help you.

Override below file

/app/design/frontend/rwd/default/template/catalog/product/list.phtml

Put below code in **<ul class="add-to-links">**

<?php
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
    $productName = $item->getProduct()->getName();
    if($productName  ==  $_product->getName()){ ?>
        <a href="<?= $this->getBaseUrl().'custommodule/index/deleteItem/id/'.$item->getId() ?>" class="link-wishlist"><?php echo $this->__('Remove form cart') ?></a>
    <?php   }
} ?>

Now in your custom controller place this function:

public function deleteItemAction() {
    $cartItemId = $this->getRequest()->getParam('id');
    $cartHelper = Mage::helper('checkout/cart');
    $cartHelper->getCart()->removeItem($cartItemId)->save();
    $this->_redirectReferer(); 
}

I have tested it's working properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top