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

1 Answer 1

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;
    }
}
6
  • 2
    This used to work, but now it does not. The First Name field has an error message though: This is a required field.
    – tread
    Apr 6, 2017 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, 2017 at 11:10
  • 1
    It is not working in Magento 2.2 throws an error This is required field
    – Moin Malek
    Nov 21, 2017 at 13:32
  • 1
    @StevieG & Moin Malek Were you guys able to find the solution for "This is a required Field error." ?
    – mighty_hk
    Jul 23, 2018 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 May 6, 2019 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.