Monday 16 December 2013

Wordpress login redirect on magento login

When user click on wordpress login it will be redirected on magento login page. After successfully login it will be redirected to wordpress site.

Add function in wp-content/themes/themefolder/functions.php

    <?php
    // For login page
    function redirect_login_page(){
       $page_viewed = basename($_SERVER['REQUEST_URI']);
       $redirect_link = base64_encode($_GET['redirect_to']);
       $home_url = base64_encode( site_url() );
        if( $page_viewed == "wp-login.php") {
          // And away they go...
           $login_page  = 'store url/customer/account/login/referer/'.$home_url; // Store url is your magento site url
           wp_redirect($login_page);
         }
         // For redirection url
         if($_GET['redirect_to'] != '') {
          $login_page  = 'store url/customer/account/login/referer/'.$redirect_link; // Store url is your magento site url
              // This condition is checked for Admin user.
              if( $_GET['redirect_to'] != 'website url/wp-admin/') {  // website url is your website site url
                 wp_redirect($login_page);
              }
         }
    }
    add_action('init','redirect_login_page');
   
    // For logout page
    function redirect_logoutpage()
    {
       $logout_page  = 'store url/customer/account/logout/'; // Store url is your magento site url
       $redirect_link = base64_encode($_GET['redirect_to']);
         if($_GET['loggedout'] || $_GET['action'] == 'logout') {
              if( $_GET['redirect_to'] != 'website url/wp-admin/') { // website url is your website site url
                 wp_redirect($logout_page);
              }
         }
    }
    add_action('init','redirect_logoutpage');
    ?>

Synchronize magento customer and wordpress user

When user login in store(magento site) it will be automatically login in website(wordpress site). If the user is registered both site with same user name and password. And also user is logout in store it will be automatically logout from website.
Please see below steps:

  • Install extension http://www.magentocommerce.com/magento-connect/magento-wordpress-integration.html in your store using connect manager.
  • Setup your website and connect with Store.
  • Now you will be able automatically login with store admin panel to website admin panel.
  • For merge customer and user login you have to changes in code like.

Add this code in index.php file which is located in root folder of store files.

    <?php
    if ( strpos($_SERVER['REQUEST_URI'], 'index.php/admin') === FALSE )
    {
        define('WP_USE_THEMES', false);
        require_once('/wp-load.php'); // Add Absolute path of wp-load.php
    }
    if (!function_exists('__')) {
        function __() {
        return Mage::app()->getTranslator()->translate(func_get_args());
        }
    }
    ?>

Then after go to /app/code/core/Mage/Core/functions.php and find
   
    <?php
    function __()
    {
        return Mage::app()->getTranslator()->translate(func_get_args());
    }
    ?>
   
And replace this

    <?php
    if (!function_exists('__')) {
        function __()
        {
            return Mage::app()->getTranslator()->translate(func_get_args());
        }
    }
    ?>
   
Then after add this function in website theme's functions.php
wp-content/themes/themefolder/functions.php

    <?php
    function login_with_email_address($username) {
        $user = get_user_by_email($username);
        if(!empty($user->user_login))
        $username = $user->user_login;
        return $username;
    }
    add_action('wp_authenticate','login_with_email_address');
    ?>
   
Then after copy this file  /app/code/core/Mage/Customer/controllers/AccountController.php and put in  /app/code/local/Mage/Customer/controllers/AccountController.php  find loginPostAction and replace with below code.

    <?php
    /**
    * Login post action
    */
    public function loginPostAction()
    {
        if ($this->_getSession()->isLoggedIn()) {
            $this->_redirect('*/*/');
            return;
        }
        $session = $this->_getSession();

        if ($this->getRequest()->isPost()) {
            $login = $this->getRequest()->getPost('login');
            if (!empty($login['username']) && !empty($login['password'])) {
                try {
                    $session->login($login['username'], $login['password']);
                    $user = login_with_email_address($login['username']);
                    $creds = array();
                    $creds['user_login'] = $login['username'];
                    $creds['user_password'] = $login['password'];
                    $creds['remember'] = true;
                    $user = wp_signon( $creds, false );
                    if ( is_wp_error($user) ) :
                        echo $user->get_error_message();
                    endif;
                     wp_set_current_user($user->ID);
                     wp_set_auth_cookie($user->ID);
                    if ($session->getCustomer()->getIsJustConfirmed()) {
                        $this->_welcomeCustomer($session->getCustomer(), true);
                    }
                } catch (Mage_Core_Exception $e) {
                    switch ($e->getCode()) {
                        case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
                            $value = Mage::helper('customer')->getEmailConfirmationUrl($login['username']);
                            $message = Mage::helper('customer')->__('This account is not confirmed. <a href="%s">Click here</a> to resend confirmation email.', $value);
                            break;
                        case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
                            $message = $e->getMessage();
                            break;
                        default:
                            $message = $e->getMessage();
                    }
                    $session->addError($message);
                    $session->setUsername($login['username']);
                } catch (Exception $e) {
                    // Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
                }
            } else {
                $session->addError($this->__('Login and password are required.'));
            }
        }
        $this->_loginPostRedirect();
    }
    ?>
   
Then after find logoutAction replace with below code.

    <?php
    public function logoutAction()
    {
        $this->_getSession()->logout()
            ->setBeforeAuthUrl(Mage::getUrl());
        wp_logout();
        $this->_redirect('*/*/logoutSuccess');
    }
    ?>
   
Now you can sucessfully login with store to automatically login with website.