5

I'm a beginner magento 2 user. Now, I want to set some default value in the shipping address's field, could you teach me what should I do? What file should I need to code?

Thank very much.

The below image is my expect result: enter image description here

8

In that case you need a plugin for this.

Create di.xml [Vendor/Module/etc/frontend/di.xml]

<?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\Block\Checkout\LayoutProcessor">
        <plugin name="vendor_assign_default_value" type="Vendor\Module\Plugin\Checkout\Model\Checkout\LayoutProcessor" sortOrder="100"/>
    </type>
</config>

Create LayoutProcessor.php [Vendor/Module/Plugin/Checkout/Model/Checkout/LayoutProcessor.php]

namespace Vendor\Module\Plugin\Checkout\Model\Checkout;


class LayoutProcessor
{
    /**
     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
    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'] = 'FirstName';

        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['shipping-address-fieldset']['children']['company']['value'] = 'Demo Company';
        return $jsLayout;
    }
}
  • 1
    This used to work, but now it does not. The First Name field has an error message though: This is a required field. – Stevie G Apr 6 '17 at 13:09
  • 1
    Hi Sohel, it works fine. But it's not working for email field. is there any solution for that? – Manish Joy Jun 2 '17 at 11:10
  • It is not working in Magento 2.2 throws an error This is required field – Moin Malek Nov 21 '17 at 13:32
  • @StevieG & Moin Malek Were you guys able to find the solution for "This is a required Field error." ? – mighty_hk Jul 23 '18 at 10:28
  • @SohelRana... I am using your SR/ModifiedChekout module..in that i wish to auto fill the billing address fields with default billing address for the logged in customer... How to do that ..Kindly help me – SUBBULAKSHMI G May 6 '19 at 7:48

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.