remove product from shopping cart in magento

remove product from shopping cart in magento

August 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
remove product from shopping cart in magento

Removing Products from Shopping Cart in Magento

Hello! I’m glad you’re looking to enhance your shopping cart functionality in Magento. In this post, we’ll walk through the process of removing items from the cart using the predefined methods available.

Understanding the Current Setup

Let’s first understand what your current cart.phtml template does:

The Remove Button Code Snippet

<th rowspan="<?php echo $mergedCells; ?>" class="a-center">
    <button type="button" class="button btn-cart"
            onclick="setLocation('<?php echo $this->getDeleteUrl(); ?>')">
        <span><span><?php echo $this->__('Remove from Cart'); ?></span></span>
    </button>
</th>

This code generates a ‘Remove from Cart’ button for each item in the cart. The setLocation JavaScript function redirects to a URL generated by the getDeleteUrl() method when clicked.

Removing an Item: A Step-by-Step Guide

To implement the removal functionality, you’ll need to leverage Magento’s built-in delete action:

1. Define the Delete Action in Your Controller

Let’s assume you have your custom cart controller named CartController. Inside this controller, define a new action called delete:

public function deleteAction()
{
    $itemId = (int)$this->getRequest()->getParam('id');
    if ($itemId) {
        try {
            $quoteItem = $this->_cart->getQuote()->getItemById($itemId);
            if ($quoteItem && $quoteItem->getId()) {
                $quoteItem->isDeleted(true);
                $this->messageManager->addSuccessMessage(__('The item has been removed from your cart.'));
            }
        } catch (Exception $e) {
            $this->messageManager->addErrorMessage(__('An error occurred while removing the item from your cart.' . $e->getMessage()));
        }
    }

    return $this->_redirect('checkout/cart');
}

In this action:

  • Retrieve the id parameter from the request, which corresponds to the item ID.
  • Check if an item exists with that ID in the current quote (cart).
  • If found, mark it as deleted using $quoteItem->isDeleted(true) and add a success message.
  • If any exception occurs during this process, log the error and redirect back to the cart page.

2. Update the Delete URL

Now, let’s update the getDeleteUrl() method in your template block:

public function getDeleteUrl()
{
    return $this->getUrl('cart/delete', ['id' => $_item->getId()]);
}

This generates a URL to the new delete action of your CartController, passing the item’s ID as a parameter.

3. Update the Cart Template

Finally, update your cart.phtml template:

<th rowspan="<?php echo $mergedCells; ?>" class="a-center">
    <button type="button" class="button btn-cart"
            onclick="setLocation('<?php echo $this->getDeleteUrl() ?>')">
        <span><span><?php echo $this->__('Remove from Cart'); ?></span></span>
    </button>
</th>

The getDeleteUrl() method now points to the correct delete action.

Testing the Removal Functionality

To test this, add a product to your cart and click on the ‘Remove from Cart’ button next to an item. The item should be removed, and you’ll see a success message indicating its removal.

Conclusion

Removing items from the shopping cart in Magento is straightforward once you understand how the built-in delete functionality works. By leveraging your custom controller’s delete action and updating the template accordingly, you can provide a seamless user experience for cart management.

Remember to keep your code clean and follow best practices for security and performance.

Happy coding! If you have any questions or need further assistance with Magento development, feel free to reach out. We’re here to help!

Link to Laravel Company

Checkout Catalog Themes JavaScript

Our network