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?

  • 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 at 3:22
1

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