3

When the user logs in, an event is triggered in the observer. Event name: customer_login

So I want to get all the cart items of that user who logged in, I did this: (I had 5 cart items, so it should loop 5 times)

$cart = Mage::getModel('checkout/cart')->getQuote();
        foreach ($cart->getAllItems() as $item) {
            Mage::log("test",null,"test.log");
        }

This same code worked everywhere, but inside the login event it does not work. Nothing gets logged, it doesn't go inside the loop at all.

Then I also tried this instead:

$cart = Mage::getSingleton('checkout/session')->getQuote()
        foreach ($cart->getAllVisibleItems() as $item) 

Same thing happened, it doesn't go inside the loop!

Is there any other alternative way?

4
  • did you enabled the persistent shopping cart ? – Christophe Ferreboeuf Oct 23 '15 at 9:13
  • nope. no idea about this. besides that i cannot edit any configuration from admin panel, my plugin has to be independent of it – user1735921 Oct 23 '15 at 9:23
  • If I understand well, you developp a module that needs to retrieve the items that were already in the cart last time the user logged in ? if yes, this thing is native to magento. about your second affirmation, I can say that a module better uses core features than redeploy what exists. to change config data programmatically : inchoo.net/magento/… – Christophe Ferreboeuf Oct 23 '15 at 9:59
  • Yeah, I want to retrieve that, I login, save products to cart, then I logout, then login again to see if the cart is same.. it is same when I do it from browser, but on user login, I can't get it in the code – user1735921 Oct 23 '15 at 10:03
1

To get all the cart items of that user who logged in, using the event customer_login try the below code in the observer function :

$session = Mage::getSingleton('customer/session');
$customerQuote = Mage::getModel('sales/quote')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->loadByCustomer($session->getCustomerId());

foreach ($customerQuote->getAllItems() as $item) {
    $product = $item->getProduct();//if you need it
    //your magic here.
}
0

To achieve that, you need to have the shopping cart set as persistent. This can be done by two ways :

Once you did that, on user login, the shopping cart that was set on logout will be avaiable. It adds the products that were on the shopping cart before login.

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.