Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top