1

I am trying to implement Buy Now feature and I find that if there are 2 items from before in cart, total of 3 items are shown in Checkout page when only 1 was expected.

All solutions that I have found in open source seem to add "buy now" product to cart first and then only checkout.

Is there a way where I can create another cart for same user ( guest / logged in ) so that this single item goes to that cart. I can subscribe to pre checkout event and then use that cart for fetching single item ?

Magento - 1.9

EDIT

I think there could be an approach where a cart full of items can be split into two orders , one for product that has been added using "buy now" and another for rest of items. Then, checking out buy now order would let checkout only 1 item. Is this correct approach since it seems checkout can not be decoupled from cart and also we can't create multiple carts !

1

The checkout uses the quote (cart) based on the quote id saved in the session. So to preserve the original cart but go to the checkout with another item, I would:

In the "buy now" action:

  1. back up current quote id in session

    $quoteId = Mage::getSingleton('checkout/session')->getQuote()->getId();
    Mage::getSingleton('checkout/session')->setData('original_quote_id', $quoteId);
    
  2. create new empty quote

    $quote = Mage::getModel('sales/quote')
    Mage::getSingleton('checkout/session')->replaceQuote($quote);
    Mage::getSingleton('checkout/cart')->setQuote($quote);
    
  3. add product to cart based on request (similar to CartController::addAction())

    $request = $this->getRequest()->getParams();
    Mage::getSingleton('checkout/cart')->addProduct($request['product'], $request);
    

After checkout is complete (using an observer)

  1. restore backed up quote

    $oldQuoteId = Mage::getSingleton('checkout/session')->getData('original_quote_id');
    if ($oldQuoteId) {
        $quote = Mage::getModel('sales/quote')->load($oldQuoteId);
        Mage::getSingleton('checkout/session')->replaceQuote($quote);
    }
    
  • thanks for solution, only thing I am bit concerned about with this approach is when user opens magento in another tab as well and then for the period check out is in progress for "buy now", he will see lesser items in cart. I think if he tries to checkout whole cart as well simultaneously from second chrome tab , he will find only 1 "buy now" item even if there were 2 other items from before. Please correct if my understanding is wrong here. – fortm Apr 8 '16 at 21:37
  • 1
    That's true and can hardly be avoided. You would need to have two different sessions in the two browser tabs. But even if you force the session id using the SID GET parameter, it will also set the id in the session cookie, so that it changes for all other tabs as well. – Fabian Schmengler Apr 11 '16 at 8:39

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.