Magento 2 place order without a Email address

This blogpost guide you a hack to place order in magento 2 via Email. As we all know email is an essential component for managing data in Magento. But today i came across to one of my client who does not want email to be necessary, i told him that this is not possible in Magento but he wanted to implement such functionality. So i came across a hack, i removed validation from client side and passed a random generated email. Here are the steps that i followed:-

To achieve this we need to override Magento_Checkout/js/action/place-order file and Magento_Checkout/template/form/element/email.html, to do that create a requirejs-config file in your module under view/frontend/requirejs-config.js

var config = {
    map: {
        '*': {
            'Magento_Checkout/template/form/element/email.html': 
            'W3solver_Trackorder/template/form/element/email.phtml',
	    'Magento_Checkout/js/action/place-order': 
            'W3solver_Trackorder/js/action/place-order'
        }
  }
};

Place your overriden file under view/frontend/web/js/action/place-order.js and add the below code. Here we have just added an optional email(emailOptional@w3solver.com), so all order will be placed by this email.

/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
define(
    [
        'Magento_Checkout/js/model/quote',
        'Magento_Checkout/js/model/url-builder',
        'Magento_Customer/js/model/customer',
        'Magento_Checkout/js/model/place-order'
    ],
    function (quote, urlBuilder, customer, placeOrderService) {
        'use strict';

        return function (paymentData, messageContainer) {
            var serviceUrl, payload, email;

            payload = {
                cartId: quote.getQuoteId(),
                billingAddress: quote.billingAddress(),
                paymentMethod: paymentData
            };

            if (customer.isLoggedIn()) {
                serviceUrl = urlBuilder.createUrl('/carts/mine/payment-information', {});
            } else {
                serviceUrl = urlBuilder.createUrl('/guest-carts/:quoteId/payment-information', {
                    quoteId: quote.getQuoteId()
                });
                
                /* added email optional*/
                if(quote.guestEmail == null) {
                    email = 'emailOptional@w3solver.com';
                }
                payload.email = email;
            }

            return placeOrderService(serviceUrl, payload, messageContainer);
        };
    }
);

Now we will look how to make email as non-required field, for that we have overridden core email.html file, place your overriden file under view/frontend/web/template/form/element/email.phtml and remove the required from email input as seen in below code:-

<div class="field">
            <label class="label" for="customer-email">
                <span data-bind="i18n: 'Email Address (Optional)'"></span>
            </label>
            <div class="control _with-tooltip">
                <input class="input-text"
                       type="email"
                       data-bind="
                            textInput: email,
                            hasFocus: emailFocused"
                       name="username"
                       data-validate="{'validate-email':true}"
                       id="customer-email" />
                <!-- ko template: 'ui/form/element/helper/tooltip' --><!-- /ko -->
                <span class="note" data-bind="fadeVisible: isPasswordVisible() == false"><!-- ko i18n: 'You can create an account after checkout.'--><!-- /ko --></span>
            </div>
        </div>

Leave a Comment.