Magento – How to update the mini-cart proceed to checkout according to customer login in magento 2

checkoutcustomermagento2.3.0mini-carturl

I Want to check the user login status in mini cart and redirect the customer to the login page when click the proceed to checkout button in mini-cart.

How to edit the minicart.js file to achieve this?

vendor\magento\module-checkout\view\frontend\web\js\view

'url': {
            'checkout': window.checkout.checkoutUrl,
            'update': window.checkout.updateItemQtyUrl,
            'remove': window.checkout.removeItemUrl,
            'loginUrl': window.checkout.customerLoginUrl,
            'isRedirectRequired': window.checkout.isRedirectRequired
        },

How to use the if condition to check the user is logged or not logged?
enter image description here

I want to forcefully redirect to the customer (registered or guest) to the login page and after logged mini cart proceeds to checkout, the button should redirect to the checkout page.

Update

I did following changes to minicart.js

define([
'uiComponent',
'Magento_Customer/js/customer-data',
'Magento_Customer/js/model/customer',//Added this line
'jquery',
'ko',
'underscore',
'sidebar',
'mage/translate',
'mage/dropdown'
], function (Component, customerData, customer, $, ko, _) { //Passed argument customer
'use strict';

//checked the customer logged or not.
if(customer.isLoggedIn()){
        alert("yes");
        var urls = window.checkout.checkoutUrl;
    }else{
        alert("no");
        var urls = window.checkout.customerLoginUrl;
    }

esidebarInitialized = true;
    miniCart.sidebar({
        'targetElement': 'div.block.block-minicart',
        'url': {
            'checkout': urls,
            'update': window.checkout.updateItemQtyUrl,
            'remove': window.checkout.removeItemUrl,
            'loginUrl': window.checkout.customerLoginUrl,
            'isRedirectRequired': window.checkout.isRedirectRequired
        }, 
................... rest of the code of the file

Unfortunately customer.isLoggedIn() this return always not logged.
How to check a customer is logged or not logged?

Best Answer

There is two option

  1. Allow Guest Checkout is No

Stores -> Configuration -> Sales -> Checkout -> Checkout Option

Allow Guest Checkout = No

Otherwise customise to sidebar.js

vendor\magento\module-checkout\view\frontend\web\js\sidebar.js

line no 63

 events['click ' + this.options.button.checkout] = $.proxy(function () {
            var cart = customerData.get('cart'),
                customer = customerData.get('customer'),
                element = $(this.options.button.checkout);

            if (!customer().firstname && cart().isGuestCheckoutAllowed === false) {
                // set URL for redirect on successful login/registration. It's postprocessed on backend.
                $.cookie('login_redirect', this.options.url.checkout);

                if (this.options.url.isRedirectRequired) {
                    element.prop('disabled', true);
                    location.href = this.options.url.loginUrl;
                } else {
                    authenticationPopup.showModal();
                }

                return false;
            }
            element.prop('disabled', true);
            location.href = this.options.url.checkout;
        }, this);
Related Topic