2

We have integration between wordpress and magento, so we have a registration from at wordpress site through which we create customer at magento side. So there is no problem to create customer from wordpress to magento using Ajax request.

But on the same time I would like to logged in magento as well.

So after registration I call the following function

$session->setCustomerAsLoggedIn($customer);

But this is not working like when I go on magento site, I am still log out.

Magento domain:- store.xyz.com wordpress domain:- xyz.com

In magento cookie domain is :- xyz.com In magento cookie path :- blank

Following is the code which I used for registration

public function signupAction() {
    $sourceUrl = str_replace('store.', '', Mage::getUrl());
    $sourceUrlWithHttps = str_replace('http', 'https', $sourceUrl);
    switch (str_replace('www.', '', $_SERVER['HTTP_ORIGIN']) . '/') {
        case $sourceUrl: case $sourceUrlWithHttps:
        header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
        header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
        header('Access-Control-Max-Age: 1000');
        header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
        break;
    }

    /** @var $session Mage_Customer_Model_Session */
    $session = $this->_getSession();
    if ($session->isLoggedIn()) {
        echo json_encode(
            array(
                'status' => 0,
                'message' => 'You are already registered with us and logged in.'
            )
        );
        die;
    }
    $session->setEscapeMessages(true); // prevent XSS injection in user input

    $customer = $this->_getCustomer();

    try {
        $errors = $this->_getCustomerErrors($customer);

        if (empty($errors)) {
            $customer->cleanPasswordsValidationData();
            $customer->save();
            Mage::dispatchEvent('customer_register_success',
                                array('account_controller' => $this, 'customer' => $customer)
            );
            if ($customer->isConfirmationRequired()) {
                /** @var $store  Mage_Core_Model_Store*/
                $store = Mage::app()->getStore();
                $customer->sendNewAccountEmail(
                         'confirmation',
                         $session->getBeforeAuthUrl(),
                         $store->getId()
                );
            } else {
                $customer->sendNewAccountEmail( 'registered', '', Mage::app()->getStore()->getId() );
                $session->setCustomerAsLoggedIn($customer);
            }
            echo json_encode(
                array(
                    'status' => 1,
                    'message' => 'success'
                )
            );
            die;
        } else {
            echo json_encode(
                array(
                    'status' => 0,
                    'message' => implode(',', $errors)
                )
            );
            die;
        }
    } catch (Mage_Core_Exception $e) {
        $session->setCustomerFormData($this->getRequest()->getPost());
        if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
            $message = $this->__('There is already an account with this email address. If you are sure that it is your email address, <a href="%s">click here</a> to get your password and access your account.', $url);
            $session->setEscapeMessages(false);
        } else {
            $message = $e->getMessage();
        }
        echo json_encode(
            array(
                'status' => 0,
                'message' => $message
            )
        );
        die;
    } catch (Exception $e) {
        echo json_encode(
            array(
                'status' => 0,
                'message' => $this->__('Cannot save the customer.')
            )
        );
        die;
    }
}

Please help to find out why I am not able to logged in.

1 Answer 1

1

This code is the problem-

    echo json_encode(
        array(
        .......
        )
    );
    die;

You shouldn't send the response on your end using die, since that will not allow magento to save the new session data to the disk. You should instead send the response using-

$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
return;

Also, though not exactly a requirement, it would be better if you also set the headers in the same way-

$this->getResponse()->setHeader('Content-type', 'application/json');
1
  • :- I tried, but it's not working.
    – Saurabh
    Commented Dec 18, 2015 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.