0

I would like to write a plugin for Magento\Customer\Controller\Account\LoginPost.

Actually I've custom template that is called when customer clicked on Proceed to Checkout. (Only when customer is not logged In)

That Template contains default login form.

After login there customer will redirect to checkout and will place order as guest.

Problem occurring when login failed at custom template. After failed login customer redirected to Core URL that is Customer/account/login that I don't want.

I want to redirect to my custom template when customer failed to login from my custom template.

So Question Is How Can I prevent redirect to default form when login failed at my custom template ?

Note : Default login page functionality should be same no need to change into it.

0

Create A before Plugin on execute() function of Magento\Customer\Controller\Account\LoginPost.

And on this plugin at setBeforeAuthUrl ($this->session->setBeforeAuthUrl() set your desire url ,where your redirect to customer after failure login .

Note $this->session is instance of Magento\Customer\Model\Session

Plugin Class

<?php

namespace Mymodule\DevAmitBera\Plugin;

use Magento\Customer\Model\Session;

class CustomerLoginPostPlugin {

    /**
     * @var Session
     */
    private $customerSession;

    public function __construct(
     Session $customerSession        
    ) {

        $this->customerSession = $customerSession;
    }

    public function beforeExecute(
    \Magento\Customer\Controller\Account\LoginPost $subject
    )
    {
        if ($subject->getRequest()->isPost()) {
            $login = $subject->getRequest()->getPost('login');
            // add new  hidden field  lasturi to your login form  to beheave loginpost
            // for other login action
            if (!empty($login['lasturi'])){
                $this->session->setBeforeAuthUrl('http://www.example.com/contact/');
            }

        }

    }
}

NOte that code is tested. Post base on idea. Also, you need to add a hidden field login[lasturi] your form to make loginpost work same as now.

  • Amit, I think you misunderstood question !! – anonymous Dec 27 '18 at 8:30
  • Please tell what i thought wrong – Amit Bera Dec 27 '18 at 8:43
  • I don't want to tell you in details because It will make you confused. Just let me know How can I prevent to redirect to default login form ? When user entered invalid details at my custom form(Custom form has same action after submitting that form data posted to core loginpost method No Custom code there.) – anonymous Dec 27 '18 at 8:54

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.