2

We are displaying Products in My account section . now we are trying to delete the product once we click on "Delete" button.

I used 2 ways to find solution :

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

enter image description here

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

        <?php foreach ($collections as $key => $product)
        {
        echo $product->getName().' <br>';
        $_product = Mage::getModel('catalog/product')->load($product->getId());    

        ?>

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

    ?>" method="post">  

    <div>   
    <button type="submit">Delete</button>  
    </div>
    </form>

   <?php
        }
    ?>   

update

2nd way : using controller to delete product , now onclick button, its redirect to url : sitename.com/example/amasty/deleteproduct?itemId= but not deleting product.

public function deleteproductAction()
    {

        $itemId = $this->getRequest()->getParam('itemId');
        if($itemId) 
        {
            Mage::helper('checkout/cart')->getCart()->removeItem($itemId)->save();           
           $this->_redirectReferer();
        }           

    }   

html

<?php $_product = Mage::getModel('catalog/product')->load($product->getId());
 ?>
<a href="#" onclick="return deleteItem('<?php $_product; ?>');">Delete</a>

script

function deleteItem(itemId) {
        if(confirm('Are you sure you want to delete this item?')) {          
           window.location = '<?php echo $this->getUrl();?>example/amasty/deleteproduct?itemId=' + itemId;
        }
        return false;
    }
  • i followed link : sarfarazlaghari.wordpress.com/2015/05/14/… – vickey colors Jun 1 '17 at 10:51
  • i also tried <a href = "<?php Mage::register('isSecureArea', true); try { $product->delete(); Mage::unregister('isSecureArea'); } catch(Exception $e) { echo "write"; } ?>"> <button><span><span>Delete</span></span></button> </a> – vickey colors Jun 1 '17 at 10:58
2

Update your code.

<?php $_product = Mage::getModel('catalog/product')->load($product->getId()); ?>
<a href="#" onclick="return deleteItem('<?php echo $_product->getId() ?>');">Delete</a>

And

public function deleteproductAction()
{
    $itemId = $this->getRequest()->getParam('itemId');
    if($itemId) 
    {
        Mage::register('isSecureArea', true);
        $_product = Mage::getModel("catalog/product")->load($itemId);
        $_product->delete();
        Mage::unregister('isSecureArea');
        $this->_redirectReferer();
    }           

}
  • Updated your code, now onclick button its redirecting to sitename.com/example/amasty/deleteproduct?itemId= , but product not deleting..... please check...... – vickey colors Jun 1 '17 at 12:18
  • Try replacing deleteItem('<?php $product->getId() ?>') with deleteItem('<?php $_product->getId() ?>') – Jaimin Sutariya Jun 1 '17 at 12:24
  • sorry, still the same...... – vickey colors Jun 1 '17 at 12:25
  • Ohh.. yes. I got the error. replace deleteItem('<?php $product->getId() ?>') with deleteItem('<?php echo $product->getId() ?>') – Jaimin Sutariya Jun 1 '17 at 12:27
  • okay, now i got : Cannot complete this operation from non-admin area. error – vickey colors Jun 1 '17 at 12:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.