2

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 Kamani Jun 14 '16 at 12:03
  • yes exactly , i want to get guest email – Samir AFALLAH Jun 14 '16 at 12:05
  • You want to get it using js? – Rakesh Jesadiya Jun 14 '16 at 13:21
  • 1
    no i want to get it using PHP on my configProvider – Samir AFALLAH Jun 14 '16 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 jones Jun 14 '16 at 20:36
5

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
    Yes your solution works fine for me – PMB Oct 27 '17 at 9:59
  • It is so cool to get this window.checkoutConfig array with almost everything we can ever need. – open-ecommerce.org May 16 at 9:25
0

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.

0
  • 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

0

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.
               }
        }

    }
0

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;
            },

        });
    }
);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.