1

I have created a new page (step) on my checkout which I'd like my users to be able to login in via. All works well when the user enters valid information (they're logged in successfully), but if they enter the wrong email / password, they are redirected to the /customer/account/login/ page. I'd like to change this URL to the /checkout/ (basically, just refreshing the page).

I have located the class (Magento\Customer\Model\Url) / method (getLoginUrl()) which sets the redirect URL. I've extended the class in a new module to overwrite the method to add an IF statement to it. I've added a check to see whether the user is on the /checkout/, and if so, return the /checkout/ as the loginUrl. If the user isn't on the checkout page (for example, just the usual customer/account/login page) then return the orginal URL set in the class as a constant. All looks like it should work well, right? Please see below for my extending class and my problem:

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Mitch\Customer\Model;

use Magento\Customer\Model\Session;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Url\EncoderInterface;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
 * Customer url model
 */
class Url extends \Magento\Customer\Model\Url
{
    /**
     * Route for customer account login page
     */
    const ROUTE_ACCOUNT_LOGIN = 'customer/account/login';

    /**
     * @var UrlInterface
     */
    public $storeId;
    /**
     * @var UrlInterface
     */
    public $currentUrl;
    /**
     * @var StoreManagerInterface
     */        
    public $storeManager;


    /**
     * @param Session $customerSession
     * @param ScopeConfigInterface $scopeConfig
     * @param RequestInterface $request
     * @param UrlInterface $urlBuilder
     * @param EncoderInterface $urlEncoder
     */
    public function __construct(
        Session $customerSession,
        ScopeConfigInterface $scopeConfig,
        RequestInterface $request,
        UrlInterface $urlBuilder,
        EncoderInterface $urlEncoder,
        StoreManagerInterface $storeManager
    ) {
        $this->storeManager = $storeManager;
        parent::__construct($customerSession, $scopeConfig, $request, $urlBuilder, $urlEncoder);
    }


    public function getBaseUrl()
    {   
        $this->storeId = 1;
        return $this->storeManager->getStore($this->storeId)->getBaseUrl();
    }

    /**
     * Retrieve customer login url
     *
     * @return string
     */
    public function getLoginUrl()
    {
        $this->baseUrl = $this->getBaseUrl();
        $this->currentUrl = $this->urlBuilder->getCurrentUrl();
        $this->checkoutUrl = $this->baseUrl.'checkout/';

        if ( $this->currentUrl == $this->checkoutUrl ) {
            echo "Yes, the user is on the checkout page";
            return $this->urlBuilder->getUrl('checkout/', $this->getLoginUrlParams());
        } 

        return $this->urlBuilder->getUrl(self::ROUTE_ACCOUNT_LOGIN, $this->getLoginUrlParams());
    }

}

Unfortunately, even though the IF statement rings True (my echo statement is echoed), the new URL (checkout/) doesn't work.

Bizarrely, if I delete my updates and just hard code in the new URL, it works..! Obviously, I can't do this, as it'll redirect the user to the checkout no matter what page they're using to login.

public function getLoginUrl()
{
    // This would work 
    return $this->urlBuilder->getUrl('checkout/', $this->getLoginUrlParams());   
}

Any ideas why the URL won't change if I set it dynamically?

Your Answer

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

Browse other questions tagged or ask your own question.