sessionmagentomagento-soap-api

Magento - Customer Not Logged in After Redirect From External Page


I'm having a lot of trouble getting a customer to stay logged in after I have created a new account. I'm creating them an account and adding products to a cart from an external site, and then sending them to Magento store. What I want to have happen is:

  1. A customer goes to a signup page and enters relevant information.
  2. They go to a new page where they can choose to add free samples to their cart.
  3. After picking samples. their account is created and they are redirected to the Magento store with the samples in their cart.

The problem I'm having is between steps 2 and 3. I create an account using the Magento's SOAP API with the following:

$customerInfo = array('firstname' => $custFirstname, 'lastname' => $custLastname, 'email' => $email, 'password_hash' => md5( $new_password ), 'store_id' => 34, 'website_id' => 25,'group_id' => 9);

// Call API method customer.create
$newCustomerId = $client->call( $session, 'customer.create', array( $customerInfo ) );

I then return their customer ID, which I pass to another function which actually logs them in and adds products to their cart:

public static function _addSamplesToCart($customer_id, $sample_one, $sample_two, $sample_three){
Mage::getSingleton("core/session", array("name" => "frontend"));
$customerSession = Mage::getSingleton('customer/session', array("name" => "frontend"));
$customerSession->init("customer_bn_us");
$customerSession->loginById($customer_id);
$cart = Mage::getSingleton('checkout/cart');
$cart->addProductsByIds($idArray);
$cart->save();
$cart->getCheckoutSession()->setCartWasUpdated(true);
}

These functions are actually called from AJAX post requests, so when I get a successful return value from _addSamplesToCart, I do a simple javascript redirect (window.location ="http://www.myhostname.net/welcome") and it correctly takes me there, but I'm not logged in.

I've been looking around all afternoon and trying a few different things and nothing has worked. I've also tried doing a hidden form submit to /customer/account/loginPost and it still doesn't work. I have verified that it's correctly creating an account in Magento. My only hunch is that I'm doing this with a mix of AJAX and PHP so somehow the cookies or sessions are getting mixed up?


Solution

  • If you are trying to login from an hostname different from the one used by the Magento store, it cannot work. You can verify this by checking if a frontend cookie is created after the AJAX call.

    I suggest you to create a new controller in Magento that receives the id of the customer just created and executes the code you wrote in your function:

    www.myhostname.net/autologin/myaction/13 (where 13 is the id of the customer just created).

    Obviously you should add something else to avoid security issues.