Situation: I have this single product cart as rules on my cart page. What I did was I detect the other product being added by the user and display it on the top of the cart. See this photo: Second Item Added Notification

I want that when you click the continue button the Product on the Cart will be Empty and Replace by the Product Listed on the Notification. Can a link like /checkout/cart//?id= do that? If not what would be the possible thing to do?
Note: This is on the Shopping Cart Page.


You will need to go a little fancy for this. Extend the cart controller in your module and add some function, lets say it singlecartAction. It will look something like this :

public function singlecartAction() {
    $cart   = $this->_getCart();
    $params = $this->getRequest()->getParams();


    try {
        if (isset($params['qty'])) {
            $filter = new Zend_Filter_LocalizedToNormalized(
                array('locale' => Mage::app()->getLocale()->getLocaleCode())
            $params['qty'] = $filter->filter($params['qty']);

        $product = $this->_initProduct();
        $related = $this->getRequest()->getParam('related_product');

        if (!$product) {

        $cart->addProduct($product, $params);
        if (!empty($related)) {
            $cart->addProductsByIds(explode(',', $related));



            array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())

        if (!$this->_getSession()->getNoCartRedirect(true)) {
            if (!$cart->getQuote()->getHasError()) {
                $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));
    } catch (Mage_Core_Exception $e) {
        if ($this->_getSession()->getUseNotice(true)) {
        } else {
            $messages = array_unique(explode("\n", $e->getMessage()));
            foreach ($messages as $message) {

        $url = $this->_getSession()->getRedirectUrl(true);
        if ($url) {
        } else {
    } catch (Exception $e) {
        $this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));

Basically, I just combined functions of emptying the cart and adding product to the cart. You can wrap some try-catch around it and remove some extra code if you want.

  • can I call the function via link? if yes what would be the link? – Alen Nov 26 '15 at 7:56
  • I guess it will be /checkout/cart/singlecart... am I right @Prateek ? – Alen Nov 26 '15 at 8:02
  • I did add the code and what I get was an empty cart only. – Alen Nov 26 '15 at 8:11
  • I did edit it @Prateek and the code works great except for the Success Message is not on the top most. – Alen Nov 26 '15 at 8:25
  • It's based on default cart controller. Success message is being added to the session which will be displayed along with any other session messages on next page load. Can you elaborate a little what exactly is the issue you are facing with this ? – Prateek Nov 26 '15 at 8:59

That's easy
Use below code in Product View Page just after $_product is defined.

To Empty the Cart


Add the Product Via Link

$_formKey= Mage::getSingleton('core/session')->getFormKey();     
$_productAddToCartLink= Mage::getUrl('checkout/cart/add',array('product'=>$_product->getId(), 'form_key'=> $_formKey));

<a href="<?php echo $_productAddToCartLink?>">Product Add To Cart Link</a>
  • /app/design/frontend/base/default/template/catalog/product/view.phtml Copy this file to your theme & put after $_product is defined. The link would be display on each product view page. – Akhil Gupta Nov 26 '15 at 7:22
  • How to trigger the cart to be empty first before adding the product? – Alen Nov 26 '15 at 7:22
  • This must happen on the Shopping Cart Page not on the Product Page. – Alen Nov 26 '15 at 7:30
  • When a new product will be added to cart. Cart will be empty automatically. – Akhil Gupta Nov 26 '15 at 7:31
  • That shouldn't be happening. – Alen Nov 26 '15 at 7:49

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.