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?
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
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