0

Please advice: I'm redirecting through JS to customer login page customer/account/login and I want that customer email field should be already filled with some value, which I provide.

1

Before you redirect to the customer/account/login, also send a Ajax call with this code:

            $.ajax ({
                type : "POST",
                url : "<?php echo $block->getUrl("some/controller/url")?>?isAjax=true",
                showLoader : true,
                data : {
                    request : 'filled_email',
                    email : 'your_custom_email',
                },
                success : function () {
                    window.location.href = "<?php echo $block->getUrl('customer/account/login')?>";
                }
            })

and in the Controller, use this:

/**
 * @var \Magento\Framework\Registry
 */
protected $_coreRegistry;

public function __construct(
        \Magento\Customer\Model\Session $session,
        ResultJsonFactory $resultJsonFactory,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\App\Action\Context $context
    ) {
        $this->_session = $session;
        $this->resultJsonFactory = $resultJsonFactory;
        $this->_coreRegistry = $registry;
        return parent::__construct($context);
    }

    public function execute()
    {
        $param = $this->getRequest()->getParams();
        $request = $param['request'];
        if ($request == 'filled_email') {
            $this->_session->setFilledEmail(true);
            $this->_session->setEmailFill($param['email']);
        }
        return $this->resultJsonFactory->create()->setData(['registry' => 'success']);
        // TODO: Implement execute() method.
    }

Finally, add your custom Block within template in customer_account_login.xml to check if the $this->_session->getFilledEmail() == true so filled the email field with the $this->_session->getEmailFill() value. Don't forget to reset the session after done.

Hope it help!

1
  • Hi Nero, thanks for your response. Initially I thought that customer auth form is generated via UI components and I could not find how. The template I was looking for is 'module-customer/view/frontend/templates/form/login.phtml'. And email field has value="<?= $block->escapeHtmlAttr($block->getUsername()) ?>". So the idea with session is correct. And for this specific case need to set into seession ->setUsername($value). Voting +1 for your time, but not marking as accepted answer. – zhartaunik Dec 12 '17 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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