2

Brief Introduction: I am trying to make custom rest api's for magento. To add a product in customer's cart, I followed following steps:

1) Log In customer(code):

public function loginUser( $email, $password){
         require_once ("app/Mage.php");
         umask(0);
         ob_start();
         session_start();
         Mage::app('default');
         Mage::getSingleton("core/session", array("name" => "frontend"));

      //   $websiteId = Mage::app()->getWebsite()->getId();
         $store = Mage::app()->getStore();
         $customer = Mage::getModel("customer/customer");
         $customer = Mage::getModel('customer/customer')->setWebsiteId(1);
         $customer->setStore($store);
         try {
             $customer->loadByEmail($email);
             $session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
             $session->login($email, $password);
          }catch(Exception $e){

          }
    } 

2) To add product to customer's cart(code):

Mage::init('default');
$id = '1'; 
$qty = '1';
$_product = Mage::getModel('catalog/product')->load($id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($_product, array('qty' => $qty));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

3) Logout Customer:

Mage::getSingleton("customer/session")->logout();

Problem: After Login I added a product into cart, it successfully added in corresponding customer's cart after that I logged out and then added a product to cart as a guest but the product was added in same(previous) QUOTE_ID and then again logged in from different account(not previous customer) and again I added a product and it again added in same QUOTE_ID. The Resultant carts of both user's are like this:

customer1: 3 products
customer2: 3 products

But they should be like this:

customer1: 1 product
customer2: 2 product

To Remove quote id, in logout I add reserveOrderId to null(generally it is used after checkout) but no solution:

Mage::getSingleton('checkout/session')->getQuote()->setReservedOrderId(null);

For more Information:

  • customer_id = 1 and quote_id = 5 returns after 1st add(Got customer ID from session)

  • customer_id = null and quote_id = 5 returns after 2nd add(after logout)

  • customer_id = 2 and quote_id = 5 returs after 3rd add(logged in from different account)
2

To reset the quote either of the following should work.

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

Mage::getSingleton('checkout/session')->setQuoteId(null);

QuoteId is checked if not empty when called on. If empty (null) a new one is generated which would return a new quote.

  • Sander I almost got the solution using cart = Mage::getModel('checkout/session')->getQuote(); with help of this I am able to get previous quote_id but it creates problem when I fetch cart products, the products price and subtotal is showing null. – Pushpendra May 22 '15 at 12:15

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.