Magento – Magento2: how to get guest informations in checkout

checkoutmagento2

Please, how to get guest informations like email, address, phone … in checkout?

Best Answer

This is the solution, you can get guest and customer informations in checkout , inject Magento_Checkout/js/model/quote on your view method.

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: 'Path/to/your/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;
            },

        });
    }
);