Thursday, 10 August 2017

Autologin : auto login your customers from magento admin

For that we need to create custom module for that.

Here Namespace is : Krupa
Module name is : Autologin

1) Create Module declaration file : Krupa_Autologin.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Krupa_Autologin>
            <active>true</active>
            <codePool>local</codePool>
        </Krupa_Autologin>
    </modules>
</config>

2) Create module's configuration file under ( // app/code/local/Krupa/Autologin/etc/config.xml)

In this file I have overrite magento admin customer edit file and create custom  front controller.

<?xml version="1.0"?>
<config>
    <modules>
        <Krupa_Autologin>
            <version>0.1.0</version>
        </Krupa_Autologin>
    </modules>
    <frontend>
        <routers>
            <autologin>
                <use>standard</use>
                <args>
                    <module>Krupa_Autologin</module>
                    <frontName>autologin</frontName>
                </args>
            </autologin>
        </routers>
     
    </frontend>
    <global>
       <blocks>
         <adminhtml>
                <rewrite>
                    <customer_edit>Krupa_Autologin_Block_Adminhtml_Customer_Edit</customer_edit>
                </rewrite>
            </adminhtml>
        </blocks>
         </global>
</config>

3)  Create a block file to add button and it's click event on following path (//app/code/local/Krupa/Autologin/Block/Adminhtml/Customer/Edit.php )

<?php
class Krupa_Autologin_Block_Adminhtml_Customer_Edit extends Mage_Adminhtml_Block_Customer_Edit
{
    public function __construct()
    {
        $this->_objectId = 'id';
        $this->_controller = 'customer';

        if ($this->getCustomerId() &&
            Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/create')) {
            $this->_addButton('order', array(
                'label' => Mage::helper('customer')->__('Create Order'),
                'onclick' => 'setLocation(\'' . $this->getCreateOrderUrl() . '\')',
                'class' => 'add',
            ), 0);
        }
        if ($this->getCustomerId()) {
            $this->_addButton('autologin', array(
                'label' => Mage::helper('customer')->__('Autologin'),
                'onclick' => 'setLocation(\'' . $this->getAutologinUrl() . '\')',
                'class' => 'add',
            ),1);
        }

        parent::__construct();

        $this->_updateButton('save', 'label', Mage::helper('customer')->__('Save Customer'));
        $this->_updateButton('delete', 'label', Mage::helper('customer')->__('Delete Customer'));

        if (Mage::registry('current_customer')->isReadonly()) {
            $this->_removeButton('save');
            $this->_removeButton('reset');
        }

        if (!Mage::registry('current_customer')->isDeleteable()) {
            $this->_removeButton('delete');
        }
    }

    public function getAutologinUrl()
    {
        return $this->getUrl('autologin/index/loginascustomer',array('customer_id' => $this->getCustomerId()));
    }
 
}

4) Now Create front controller on following path . (\\app\code\local\Krupa\Autologin\controllers\IndexController.php )

<?php
class Krupa_Autologin_IndexController extends Mage_Core_Controller_Front_Action
{
    public function loginascustomerAction()
    {
        
  $session = Mage::getSingleton('customer/session');
         $id = (int) trim($this->getRequest()->getParam('customer_id'));
       try{ 
        if($id){
                    $customer = Mage::getModel('customer/customer')->load($id);
                    $session->setCustomerAsLoggedIn($customer);
                    }else{
                    throw new Exception ($this->__('The login attempt was unsuccessful. Some parameter is missing'));
                }
            }catch (Exception $e){
                $session->addError($e->getMessage());
            }
           
           $this->_redirect('customer/account/');
    }
}


That's it. Hope It may helpful to you :) 








No comments:

Post a Comment