简体   繁体   English

Magento-从外部页面重定向后客户未登录

[英]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. 我正在为其创建一个帐户,并将产品从外部站点添加到购物车,然后将其发送到Magento商店。 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. 他们的帐户已创建,并将他们的购物车中的样品重定向到Magento商店。

The problem I'm having is between steps 2 and 3. I create an account using the Magento's SOAP API with the following: 我遇到的问题在第2步和第3步之间。我使用Magento的SOAP API通过以下方式创建一个帐户:

$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: 然后,我返回他们的客户ID,然后将其传递给另一个函数,该函数实际上将他们登录,并将产品添加到他们的购物车中:

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. 这些函数实际上是从AJAX发布请求中调用的,因此,当我从_addSamplesToCart获得成功的返回值时,我会执行一个简单的javascript重定向(window.location =“ http://www.myhostname.net/welcome”),并且正确地我在那儿,但我没有登录。

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. 我也尝试过将隐藏的表单提交到/ customer / account / loginPost,但仍然无法正常工作。 I have verified that it's correctly creating an account in Magento. 我已验证它在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? 我唯一的直觉是我正在使用AJAX和PHP进行混合,因此以某种方式将cookie或会话混淆了吗?

If you are trying to login from an hostname different from the one used by the Magento store, it cannot work. 如果您尝试使用的主机名不同于Magento存储所使用的主机名,则该主机名将无法正常工作。 You can verify this by checking if a frontend cookie is created after the AJAX call. 您可以通过检查在AJAX调用之后是否创建前端cookie来验证这一点。

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: 我建议您在Magento中创建一个新的控制器,该控制器接收刚刚创建的客户的ID,并执行您在函数中编写的代码:

www.myhostname.net/autologin/myaction/13 (where 13 is the id of the customer just created). www.myhostname.net/autologin/myaction/13(其中13是刚创建的客户的ID)。

Obviously you should add something else to avoid security issues. 显然,您应该添加其他内容以避免安全问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM