1

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.

2 Answers 2

2

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();

    Mage::getSingleton('checkout/cart')->truncate();

    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) {
            $this->_goBack();
            return;
        }

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

        $cart->save();

        $this->_getSession()->setCartWasUpdated(true);

        Mage::dispatchEvent('checkout_cart_add_product_complete',
            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()));
                $this->_getSession()->addSuccess($message);
            }
            $this->_goBack();
        }
    } catch (Mage_Core_Exception $e) {
        if ($this->_getSession()->getUseNotice(true)) {
            $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
        } else {
            $messages = array_unique(explode("\n", $e->getMessage()));
            foreach ($messages as $message) {
                $this->_getSession()->addError(Mage::helper('core')->escapeHtml($message));
            }
        }

        $url = $this->_getSession()->getRedirectUrl(true);
        if ($url) {
            $this->getResponse()->setRedirect($url);
        } else {
            $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
        }
    } catch (Exception $e) {
        $this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));
        Mage::logException($e);
        $this->_goBack();
    }
}

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.

6
  • can I call the function via link? if yes what would be the link?
    – Alen
    Nov 26, 2015 at 7:56
  • I guess it will be /checkout/cart/singlecart... am I right @Prateek ?
    – Alen
    Nov 26, 2015 at 8:02
  • I did add the code and what I get was an empty cart only.
    – Alen
    Nov 26, 2015 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, 2015 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, 2015 at 8:59
1

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

To Empty the Cart

Mage::getSingleton('checkout/cart')->truncate();
Mage::getSingleton('checkout/session')->clear();

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>
5
  • /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. Nov 26, 2015 at 7:22
  • How to trigger the cart to be empty first before adding the product?
    – Alen
    Nov 26, 2015 at 7:22
  • This must happen on the Shopping Cart Page not on the Product Page.
    – Alen
    Nov 26, 2015 at 7:30
  • When a new product will be added to cart. Cart will be empty automatically. Nov 26, 2015 at 7:31
  • That shouldn't be happening.
    – Alen
    Nov 26, 2015 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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