0

We are displaying Products in My account section & gave an option to add product to cart once user click on Add to cart. now we want to provide an option to delete the products once we click on "Delete" button.

[1]: https://i.stack.imgur.com/wmc

Edit

After using below code, once i click on "Delete" button, all products are deleting instead of one product :

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>

<?php $collections = $this->getCollection(); ?>

<?php $_product = $this->getProduct(); ?>

<div id="messages_product_view">
<?php $buttonTitle = Mage::helper('core')->quoteEscape($this->__('Add to Cart')); ?>
</div>

<?php foreach ($collections as $key => $product) {

    echo $product->getName().' <br>';
     echo $product->getId().'<br />';

    $_product = Mage::getModel('catalog/product')->load($product->getId());    

?>

<?php

$itemRemoveUrl=$this->getUrl('checkout/cart/delete',array('id'=>$_product->getId(),
   Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED=>$this->helper('core/url')->getEncodedUrl())
);
?>

<!-- delete -->


<form 
action="<?php  

Mage::register('isSecureArea', true);
$_product = Mage::getModel('catalog/product')->load($product->getId());
try
 {
$product->delete();
Mage::unregister('isSecureArea');
} 
catch(Exception $e) 
{
echo "write";
}

?>" method="post">  
<div class="add-to-cart-buttons">

<button type="submit" title="Delete" class="button btn-cart" >
    <span><span>Delete</span></span>
</button>

</div>
</form>

<!-- delete code end-->


<?php

}
?>
4
  • How did you add code for delete button? there is no code for delete button in above code. May 31, 2017 at 12:15
  • @JaiminSutariya i followed link & used this code : try { $product->delete(); Mage::unregister(‘isSecureArea’); } catch(Exception $e) { echo “Product #”.$product->getId().” could not be remvoved: “.$e->getMessage(); } , but it didt worked for me , may be i done something wrong in placing code...... please guide me on this.....
    – user52996
    May 31, 2017 at 12:18
  • Where and how did you use above code? May 31, 2017 at 12:20
  • @JaiminSutariya i got result Product #195652 could not be remvoved: Cannot complete this operation from non-admin area. seems there is no link b/w Delete button and that code, so only it didt worked, please tell me how to link to delete button....
    – user52996
    May 31, 2017 at 12:25

1 Answer 1

0

You can implement a button that uses that variable called $itemRemoveUrl like a link.

Cart remove URL

$itemRemoveUrl=$this->getUrl('checkout/cart/delete',array('id'=>$_product->getId(),
   Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED=>$this->helper('core/url')->getEncodedUrl())
);

Sample:

<a href="<?php echo $itemRemoveUrl ?>" class="btn btn-default"><?php echo $this->__('Delete') ?></a>
7
  • i tried like this : <?php $itemRemoveUrl=$this->getUrl('checkout/cart/delete',array('id'=>$_product->getId(), Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED=>$this->helper('core/url')->getEncodedUrl()) ); ?> <button class = "button btn-cart"><?php echo $itemRemoveUrl ?></button> is it correct ?
    – user52996
    May 31, 2017 at 12:17
  • it's correct, you just need to add this URL to your new button. May 31, 2017 at 12:27
  • i added url in this way : <button class = "button btn-cart"><?php echo $itemRemoveUrl ?></button> is this correct , please tell me what else code i need to use ?
    – user52996
    May 31, 2017 at 12:37
  • okay , i did exactly as you said : <?php $itemRemoveUrl=$this->getUrl('checkout/cart/delete',array('id'=>$_product->getId(), Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED=>$this->helper('core/url')->getEncodedUrl()) ); ?> <br><br> <button class = "button btn-cart"><a href ="<?php echo $itemRemoveUrl ?>"> Button</button> , but still onclick button, its not deleting product....
    – user52996
    May 31, 2017 at 12:48
  • I've added a sample to implement May 31, 2017 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy