I would like to call the existing authentication modal popup built into Magento 2 from a custom module. It must already be in the body somewhere since it can readily be called by clicking the checkout button when not logged in (guest checkout is disabled).
I found this javascript code in my source,
**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
/*jshint browser:true jquery:true*/
/*global alert*/
define(
[
'jquery',
'ko',
'Magento_Ui/js/form/form',
'Magento_Customer/js/action/login',
'Magento_Customer/js/customer-data',
'Magento_Customer/js/model/authentication-popup',
'mage/translate',
'mage/url',
'Magento_Ui/js/modal/alert',
'mage/validation'
],
function(,ドル ko, Component, loginAction, customerData, authenticationPopup, $t, url, alert) {
'use strict';
return Component.extend({
registerUrl: window.authenticationPopup.customerRegisterUrl,
forgotPasswordUrl: window.authenticationPopup.customerForgotPasswordUrl,
autocomplete: window.checkout.autocomplete,
modalWindow: null,
isLoading: ko.observable(false),
defaults: {
template: 'Magento_Customer/authentication-popup'
},
initialize: function() {
var self = this;
this._super();
url.setBaseUrl(window.authenticationPopup.baseUrl);
loginAction.registerLoginCallback(function() {
self.isLoading(false);
});
},
/** Init popup login window */
setModalElement: function (element) {
if (authenticationPopup.modalWindow == null) {
authenticationPopup.createPopUp(element);
}
},
/** Is login form enabled for current customer */
isActive: function() {
var customer = customerData.get('customer');
return customer() == false;
},
/** Show login popup window */
showModal: function() {
if (this.modalWindow) {
$(this.modalWindow).modal('openModal');
} else {
alert({
content: $t('Guest checkout is disabled.')
});
}
},
/** Provide login action */
login: function(loginForm) {
var loginData = {},
formDataArray = $(loginForm).serializeArray();
formDataArray.forEach(function (entry) {
loginData[entry.name] = entry.value;
});
if($(loginForm).validation()
&& $(loginForm).validation('isValid')
) {
this.isLoading(true);
loginAction(loginData, null, false);
}
}
});
}
);
Then tried to call it from a link in my custom.phml file:
<p><a onClick="openLoginPopup()" href="javascript:void(0);">Log in</a> or <a onClick="openLoginPopup()" href="javascript:void(0);">create an account</a> for additional features.</p>
This just gave me an error that top wasn't defined. How can I call this popup from a custom module?
-
Where is that code from? I could not locate in magento.Purushotam Sangroula– Purushotam Sangroula2018年01月07日 19:07:18 +00:00Commented Jan 7, 2018 at 19:07
-
it's in the right click "view source" code on my product view page(blank theme). I was hoping to just find whatever called the popup and replicate it.Front_End_Dev– Front_End_Dev2018年01月07日 19:22:33 +00:00Commented Jan 7, 2018 at 19:22
-
So you want to create custom popup?Purushotam Sangroula– Purushotam Sangroula2018年01月08日 01:33:21 +00:00Commented Jan 8, 2018 at 1:33
-
@Anime, not a custom popup, I want to call the popup that's already there. I just want to do it from a new button.Front_End_Dev– Front_End_Dev2018年01月08日 01:36:32 +00:00Commented Jan 8, 2018 at 1:36
-
Okay, I would really like to help you but I could not find the kind of popup you have shown in the pic. Would you tell me which page and when you want to modify. I tried activating blank theme and my checkout page is differnt than yours. How many checkout steps do you have?Purushotam Sangroula– Purushotam Sangroula2018年01月08日 02:27:40 +00:00Commented Jan 8, 2018 at 2:27
1 Answer 1
This is a tricky solution if you want to reuse Magento authentication popup.
In your
custom.phtml, define class name to trigger popup (E.g.trigger-auth-popup)<p> <a class="trigger-auth-popup" href="javascript:void(0);">Log in</a> or <a class="trigger-auth-popup" href="javascript:void(0);">create an account</a> for additional features. </p>Override
vendor/magento/module-customer/view/frontend/web/js/model/authentication-popup.jsby copying it to your custom theme
app/design/frontend/<Vendor>/<theme>/Magento_Customer/web/js/model/authentication-popup.jsand change the
triggerelement like this/** * Create popUp window for provided element * * @param {HTMLElement} element */ createPopUp: function (element) { var options = { 'type': 'popup', 'modalClass': 'popup-authentication', 'focus': '[name=username]', 'responsive': true, 'innerScroll': true, 'trigger': '.proceed-to-checkout, .trigger-auth-popup', // Add your custom class name to this property 'buttons': [] }; this.modalWindow = element; modal(options, $(this.modalWindow)); },Re-deploy static content and check it out :)
-
Popup shows even after you get logged in and click to login link.Kapil Yadav– Kapil Yadav2018年11月20日 09:24:10 +00:00Commented Nov 20, 2018 at 9:24
-
@kapilyadav Do you find a solution for it?David Duong– David Duong2019年09月03日 14:21:32 +00:00Commented Sep 3, 2019 at 14:21
-
Yes, actually after re-deploy static content, it worked.Kapil Yadav– Kapil Yadav2019年09月05日 06:06:16 +00:00Commented Sep 5, 2019 at 6:06
-
Explore related questions
See similar questions with these tags.