1

how to add a custom customer attributes under shipping address in checkout page in Magento2?

i m using this extension for customer attributes.

In customer registration page

enter image description here

In Shipping Page

enter image description here

  • can you please where you are looking for the custom attribute. – Hitesh Vaghasiya Jun 22 at 10:24
  • i already have a customer attribute in customer registration page prnt.sc/o57l4c – divya sekar Jun 22 at 10:33
  • now i need a customer attribute in checkout register page – divya sekar Jun 22 at 10:34
  • have you tried $usedInForms = array( 'checkout_register', ); in installData – Ghulam.M Jun 24 at 8:16
  • i tried this its not works – divya sekar Jun 24 at 10:09
2
+50

step1 : try to create a plugin for the \Magento\Checkout\Block\Checkout\LayoutProcessor::process method. because billing and shipping address forms are generated dynamically.

app/code/Adamsmage/Customattribute/Module/etc/frontend/di.xml

<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="add_custom_field_checkout_form" type="Adamsmage\Customattribute\Module\Model\Plugin\Checkout\LayoutProcessor" sortOrder="100"/>
    </type>
</config>

step2 : Create plugin class on this Directory.

app\code\Adamsmage\Customattribute\Module\Model\Plugin\Checkout

<?php
namespace Adamsmage\Customattribute\Module\Model\Plugin\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']['custom_field'] = [
            'component' => 'Magento_Ui/js/form/element/abstract',
            'config' => [
                'customScope' => 'shippingAddress.custom_attributes',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/input',
                'options' => [],
                'id' => 'custom-field'
            ],
            'dataScope' => 'shippingAddress.custom_attributes.custom_field',
            'label' => 'Checkout Custom Field',
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' => [],
            'sortOrder' => 250,
            'id' => 'custom-field'
        ];


        return $jsLayout;
    }
}
  • it will add a static field but i need to ad a dynamic field in checkout page – divya sekar Jun 28 at 7:16

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.