0

How do I correctly load a customer session by ID?

Sometimes (sporadically), Magento 1 creates a new session when returning from Payment gateway.

I want to update information to the customer' order which just did a payment.

TLDR: Session is unavailable sometimes, so $order->sendNewOrderEmail(); will throw an exception because no payment system is attached to order (no session). Another reason to ensure session is 'logged in again' is so that customer can immediately see their order/account details.

I will have the real order number from the payment system.

   if(! Mage::getSingleton('customer/session')->isLoggedIn()){
       // Load order by real order number (not 'database' id)
       // I think this is correct?
       $order = Mage::getModel('sales/order')->loadByIncrementId($OrderNumber);
       
       $customer = $order->getCustomer();
       $customerId = $order->getCustomerId();  /// OR $customer->getId();

       // I want to log in customer again, so that they will be able to see the payment updated information.
       // The current code does not seem to update session ID and load customer details, and also definitely not logged in!
       $session = Mage::getSingleton('customer/session', array('name' => 'frontend'));
       $session->setSessionId($id);
       $session->setCustomerAsLoggedIn($customerId); 
       
$order->addStatusToHistory(Mage_Sales_Model_Order::STATE_PAYMENT_REVIEW, "Review message");
$order->save();

    // Since no session is available, order->sendNewOrderEmail() has no payment method attach to it!.... so this will throw an exception!

   $order->sendNewOrderEmail();
   }

Is this possible?

I want to log in customer again, so that they will be able to see the payment updated information. Otherwise customer will not be sure about the payment details of the order - as it cannot be added to order...

Also, What is the difference between: when do you use which?

   core/session
   customer/session
   checkout/session

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.