Please, how to get guest email address in checkout?
-
I think you want to say that customer come to your site without login . then you need him/his email address from browser's session like this ? Dose this customer visit your site earlier or first time user?– Pratik KamaniCommented Jun 14, 2016 at 12:03
-
yes exactly , i want to get guest email– Samir AFALLAHCommented Jun 14, 2016 at 12:05
-
You want to get it using js?– Rakesh JesadiyaCommented Jun 14, 2016 at 13:21
-
1no i want to get it using PHP on my configProvider– Samir AFALLAHCommented Jun 14, 2016 at 13:26
-
You can't get guest customer email, before customer fill it. Then customer set email in related field, you can get it from input and send to server to store it where you need.– joni jonesCommented Jun 14, 2016 at 20:36
5 Answers
In your custom-method.js file
define(
[
'jquery',
'Magento_Checkout/js/model/quote'
],
function ($,quote,Component,) {
'use strict';
return Component.extend({
defaults: {
template: 'Namespace_ModuleName/custompayment/form_template'
},
getGuestEmail: function () {
return quote.guestEmail;
}
});
}
);
After that just call that function you will get guest email address If you want register customer email address then use following:
getEmail: function () {
return window.checkoutConfig.customerData.email;
}
-
1
-
1It is so cool to get this window.checkoutConfig array with almost everything we can ever need. Commented May 16, 2019 at 9:25
-
I place an order a guest user, In my case i need that given email id in payment page, i tried the above code but it's giving null. any idea on this?– senthilCommented Nov 18, 2022 at 14:03
You can get it using javascript in checkout page using below method,
if(!window.checkoutConfig.quoteData.customer_id){
var getGuestEmail = window.checkoutConfig.quoteData.customer_email;
alert(getGuestEmail);
}
Using above code you can get guest email id. I haven't check with php methods.
- In case of the Guest customer, magento didn't map email id to shipping or billing address before place order.
- In my case, I need guest email which is provided at the time of shipping page. To validate Guest email with Coupon code using this event ( salesrule_validator_process ) .
To Do that : Step 1: Create di.xml to override core code.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Model\AccountManagement" type="Custom\Couponfriend\Model\AccountManagement" />
</config>
Step 2: Override model in custom module, and set email to
Quote's->shipping object. and get where you need it.
<?php
namespace Custom\Couponfriend\Model;
class AccountManagement extends \Magento\Customer\Model\AccountManagement {
public function isEmailAvailable($customerEmail, $websiteId = null) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
$shippingAddress = $cart->getQuote()->getShippingAddress();
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/guestFriendsFamily.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('------------');
try {
if ($shippingAddress) {
$shippingAddress->setData('email', $customerEmail);
$shippingAddress->save();
}
} catch (NoSuchEntityException $e) {
$logger->info($e->getMessage());
}
try {
if ($websiteId === null) {
$websiteId = $this->storeManager->getStore()->getWebsiteId();
}
$this->customerRepository->get($customerEmail, $websiteId);
return false;
} catch (NoSuchEntityException $e) {
return true;
}
}
}
?>
You can override constructor to create cart object using dependency injection
This work for Magento 1
<?php
class Exinent_Billingtax_Model_Observer
{
public function autoRegisterBilling($evt){
if(!Mage::helper('customer')->isLoggedIn()){
$data =$evt->getEvent()->getControllerAction()->getRequest()->getPost('billing', array());
echo "<pre>";
print_r($data);
$customer = Mage::getModel("customer/customer");
$email = $data['email'];
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
$pwd = $data['customer_password'];
$customer->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail($email);
if (!$customer->getId()) {
//Code begins here for new customer registration
$customer->website_id = $websiteId;
$customer->setStore($store);
$customer->firstname = $data['firstname'];
$customer->lastname = $data['lastname'];
$customer->setEmail($email);
$customer->setPassword($pwd);
$customer->sendNewAccountEmail('confirmed');
$customer->save();
}
Mage::getSingleton('customer/session')->loginById($customer->getId()); // to login that customer.
}
}
}
You can get guest and customer other information in you custom js like customerinfo.js in Magento_Checkout/js/model/quote
define(
[
'jquery',
'Magento_Checkout/js/view/payment/default',
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/action/place-order',
],
function ($,Component,quote,placeOrderAction) {
'use strict';
return Component.extend({
defaults: {
template: 'Company_Module/custompayment/form_template'
},
getEmail: function () {
if(quote.guestEmail) return quote.guestEmail;
else return window.checkoutConfig.customerData.email;
},
getLastName: function () {
return quote.billingAddress().lastname;
},
getFirstName: function () {
return quote.billingAddress().firstname;
},
getAddress: function () {
return quote.billingAddress().street[0]+", "+quote.billingAddress().postcode+" "+quote.billingAddress().city+", Maroc";
},
getPhone: function () {
return quote.billingAddress().telephone;
},
});
}
);