主页加载 Magento 时 Ajax 弹出框中的登录页面

问题描述 投票:0回答:1

我们如何在 Magento 的主页加载时在 Ajax 弹出框中显示登录页面,弹出框只需要出现一次,直到浏览器关闭,我还需要将 FB 连接到同一个弹出框。请尽快回复我

php magento-1.5
1个回答
0
投票

您好,您可以使用任何灯箱插件来显示代码,并在弹出窗口中显示登录信息,您可以像这样覆盖客户 AccountController.php

  protected $_validActions = array('create','login','logoutSuccess','forgotpassword','forgotpasswordpost','confirm','confirmation','resetpassword','resetpasswordpost');
  protected $_customActions = array('signupformpopup','ajaxLogin','ajaxCreate');

 public function preDispatch()
 {

         // a brute-force protection here would be nice

       $action = $this->getRequest()->getActionName();


       if (preg_match('/^('.$this->_getCustomActions().')/i', $action))
       {
        $this->getRequest()->setActionName($this->_validActions[1]);
       }

       parent::preDispatch();

       if ($action != $this->getRequest()->getActionName())
       {
        $this->getRequest()->setActionName($action);
       }

       if (!$this->getRequest()->isDispatched()) {
        return;
       }

       if (!preg_match('/^('.$this->_getValidActions().')/i', $action)) {
        if (!$this->_getSession()->authenticate($this)) {
         $this->setFlag('', 'no-dispatch', true);
        }
       } else {
        $this->_getSession()->setNoReferer(true);
       }

 }
  protected function _getValidActions()
  {
  return implode("|", array_merge($this->_validActions, $this->_customActions));
  }

 protected function _getCustomActions()
 {
  return implode("|", $this->_customActions);
 }


/**
 * Login post action
 */
public function ajaxLoginAction() {

    if ($this->_getSession()->isLoggedIn()) {
        $this->_redirect('*/*/');
        return;
    }
    $session = $this->_getSession();
    $result=array();

    if ($this->getRequest()->isPost()) {
        $login = $this->getRequest()->getPost('login');
        if (!empty($login['username']) && !empty($login['password'])) {
            try {
                $session->login($login['username'], $login['password']);
                if ($session->getCustomer()->getIsJustConfirmed()) {
                    $this->_welcomeCustomer($session->getCustomer(), true);
                }
                $result['success'] = true;
                $result['redirecturl'] = Mage::getUrl('customer/account/edit');


            } catch (Mage_Core_Exception $e) {
                switch ($e->getCode()) {
                    case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
                        /*$message = Mage::helper('customer')->__('This account is not confirmed. <a href="%s">Click here</a> to resend confirmation email.', Mage::helper('customer')->getEmailConfirmationUrl($login['username']));*/
                        $result['success'] = false;
                        $result['message'] = Mage::helper('customer')->__('This account is not confirmed.');
                        break;
                    case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
                        $message = $e->getMessage();
                        $result['success'] = false;
                        $result['message'] = Mage::helper('customer')->__($message);
                        break;
                    default:
                        $message = $e->getMessage();
                        $result['success'] = false;
                        $result['message'] = Mage::helper('customer')->__($message);
                }
                //$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.'));
            $result['success'] = false;
            $result['message'] = Mage::helper('customer')->__('Login and password are required.');
        }
    }
    $this->getResponse()->setBody(Zend_Json::encode($result));
    //$this->_loginPostRedirect();
}

/**
 * Login post action
 */
public function ajaxCreateAction()
{
    $session = $this->_getSession();
    if ($session->isLoggedIn()) {
        $this->_redirect('*/*/');
        return;
    }
    $session->setEscapeMessages(true); // prevent XSS injection in user input
    if ($this->getRequest()->isPost()) {
        $errors = array();

        if (!$customer = Mage::registry('current_customer')) {
            $customer = Mage::getModel('customer/customer')->setId(null);
        }

        /* @var $customerForm Mage_Customer_Model_Form */
        $customerForm = Mage::getModel('customer/form');
        $customerForm->setFormCode('customer_account_create')
            ->setEntity($customer);

        $customerData = $customerForm->extractData($this->getRequest());

        if ($this->getRequest()->getParam('is_subscribed', false)) {
            $customer->setIsSubscribed(1);
        }

        /**
         * Initialize customer group id
         */
        $customer->getGroupId();

        if ($this->getRequest()->getPost('create_address')) {
            /* @var $address Mage_Customer_Model_Address */
            $address = Mage::getModel('customer/address');
            /* @var $addressForm Mage_Customer_Model_Form */
            $addressForm = Mage::getModel('customer/form');
            $addressForm->setFormCode('customer_register_address')
                ->setEntity($address);

            $addressData    = $addressForm->extractData($this->getRequest(), 'address', false);
            $addressErrors  = $addressForm->validateData($addressData);
            if ($addressErrors === true) {
                $address->setId(null)
                    ->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))
                    ->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false));
                $addressForm->compactData($addressData);
                $customer->addAddress($address);

                $addressErrors = $address->validate();
                if (is_array($addressErrors)) {
                    $errors = array_merge($errors, $addressErrors);
                }
            } else {
                $errors = array_merge($errors, $addressErrors);
            }
        }

        try {
            $customerErrors = $customerForm->validateData($customerData);
            if ($customerErrors !== true) {
                $errors = array_merge($customerErrors, $errors);
            } else {
                $customerForm->compactData($customerData);
                $customer->setPassword($this->getRequest()->getPost('password'));
                $customer->setConfirmation($this->getRequest()->getPost('confirmation'));
                $customerErrors = $customer->validate();
                if (is_array($customerErrors)) {
                    $errors = array_merge($customerErrors, $errors);
                }
            }

            $validationResult = count($errors) == 0;
            $result = array();
            if (true === $validationResult) {
                $customer->save();

                if ($customer->isConfirmationRequired()) {
                    $customer->sendNewAccountEmail('confirmation', $session->getBeforeAuthUrl());
                   // $session->addSuccess($this->__('Account confirmation is required. Please, check your email for the confirmation link. To resend the confirmation email please <a href="%s">click here</a>.', Mage::helper('customer')->getEmailConfirmationUrl($customer->getEmail())));
                    //$this->_redirectSuccess(Mage::getUrl('*/*/index', array('_secure'=>true)));
                    //return;
                    $result['success'] = true;
                    $result['message'] = $this->__('Account confirmation is required. Please, check your email for the confirmation link. To resend the confirmation email please <a href="%s">click here</a>.', Mage::helper('customer')->getEmailConfirmationUrl($customer->getEmail()));
                } else {
                    $session->setCustomerAsLoggedIn($customer);
                    $url = $this->_welcomeCustomer($customer);
                    //$this->_redirectSuccess($url);
                    //return;
                    $result['success'] = true;
                    $result['message'] = $this->__('You are successfully registered');
                }
            } else {
                $session->setCustomerFormData($this->getRequest()->getPost());
                if (is_array($errors)) {
                    $result['success'] = false;
                    foreach ($errors as $errorMessage) {
                        //$session->addError($errorMessage);
                        $result['message'] .= $errorMessage;
                    }
                } else {
                    //$session->addError($this->__('Invalid customer data'));
                    $result['success'] = false;
                    $result['message'] = $this->__('Invalid customer data');
                }
            }
        } catch (Mage_Core_Exception $e) {
            $session->setCustomerFormData($this->getRequest()->getPost());
            if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
                $url = Mage::getUrl('customer/account/forgotpassword');
                $result['success'] = false;
                $result['message'] = $this->__('There is already an account with this email address. If you are sure that it is your email address.');

            } else {
                $result['success'] = false;
                $result['message'] = $e->getMessage();

            }
            //$session->addError($message);
        } catch (Exception $e) {
           // $session->setCustomerFormData($this->getRequest()->getPost())
             //   ->addException($e, $this->__('Cannot save the customer.'));
            $result['success'] = false;
            $result['message'] = $this->__('Cannot save the customer.');
        }
    }

    //$this->_redirectError(Mage::getUrl('*/*/create', array('_secure' => true)));
    $this->getResponse()->setBody(Zend_Json::encode($result));
}

}

© www.soinside.com 2019 - 2024. All rights reserved.