2

I am using Magento 2.3. I am trying to pre-fill fields in Magento checkout form.
I have done following. I followed this article.

First I defined dependency injection at vendor/module/etc/frontend/di.xml.
I believe it is working.

After that, I created layoutprocessor at Vendor/Module/Plugin/Magento/Checkout/Block/Checkout/LayoutProcessor.php with following contents:

<?php

namespace Vendor\Module\Plugin\Magento\Checkout\Block\Checkout;

class LayoutProcessor 
{
    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
        array $jsLayout
    ) {
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['firstname']['value'] = 'First Name';
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['customer-email']['value'] = 'default@example.com';
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['street']['children'][0]['value'] = 'Street Line one';

        return $jsLayout;
    }
}

My problem is that, the firstname field gets filled with the value, while customer-email field does not get filled with the supplied value. Where am I doing wrong?

1
  • change this your email line with this one $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['customer-email']['value'] = 'default@example.com' – fmsthird Mar 14 '19 at 3:22
3

Instead of create plugin on \Magento\Checkout\Block\Checkout\LayoutProcessor

Create plugin on Magento\Checkout\Model\DefaultConfigProvider and use after plugin method on getConfig prefill email id But avoid it on loggedin customer as it already has email.

di.xml code:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Model\DefaultConfigProvider">
        <plugin disabled="false" name="StackExchange_MagentoAnswer_Plugin_Magento_Checkout_Model_DefaultConfigProvider" sortOrder="10" type="StackExchange\MagentoAnswer\Plugin\Magento\Checkout\Model\DefaultConfigProvider"/>
    </type>
</config>

Plugin Class

<?php

namespace StackExchange\MagentoAnswer\Plugin\Magento\Checkout\Model;

use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Customer\Model\Context as CustomerContext;
class DefaultConfigProvider
{
    /**
     * @var HttpContext
     */
    private $httpContext;

    public function __construct(
        HttpContext $httpContext
    ) {
        $this->httpContext = $httpContext;
    }

    public function afterGetConfig(
        \Magento\Checkout\Model\DefaultConfigProvider $subject,
        $result
    ) {
        if (!$this->isCustomerLoggedIn() && is_array($result)) {
            $result['validatedEmailValue'] = 'john.deo@gmail.com';
        }
        return $result;
    }
    /**
     * Check if customer is logged in
     *
     * @return bool
     * @codeCoverageIgnore
     */
    private function isCustomerLoggedIn()
    {
        return (bool)$this->httpContext->getValue(CustomerContext::CONTEXT_AUTH);
    }
}
1

For all those who are looking to autofill email, i did it by adding a phtml file on checkout.

I had guest email in customer session and wants to prefill on checkout, (you can also save that in session) i created a block which pass this email from session to phtml. (Please refer other docs to do this if you don't know)

app/design/frontend/Company/Theme/Magento_Checkout/layout/ checkout_index_index.xml

Add following code:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Company\Module\Block\EmailFill" name="content.options" template="Company_Module::autofillemail.phtml" before="checkout.root" cacheable="false"/>
        </referenceContainer>
    </body>
</page>

in app/code/Company/Module/view/frontend/templates/autofillemail.phtml

<?php

 //take guest email from block

 $email = $block->getGuestEmail(); //define this function in your block, code not in this answer DIY

 $guestEmail = isset($email) ? $email : '';
?>

<script type="text/javascript">
    require(["jquery","domReady!"], function ($) {
        $(document).ajaxComplete(function() {
            var email = "<?php echo $guestEmail; ?>";
            if (email !== "") {
                jQuery("#customer-email").val(email);
            }
        });
    });
</script>

Hope this will Help Someone and save their time!

0

Try changing your email line of code to look similar with the firstname:

<?php

namespace Vendor\Module\Plugin\Magento\Checkout\Block\Checkout;

class LayoutProcessor 
{
    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
        array $jsLayout
    ) {
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['firstname']['value'] = 'First Name';
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['customer-email']['value'] = 'default@example.com';
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['street']['children'][0]['value'] = 'Street Line one';

        return $jsLayout;
    }
}  

You are just missing the ['shipping-address-fieldset']['children']

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.