cancel
Showing results for 
Search instead for 
Did you mean: 

Checkout: add a billing address (ONLY) field

Checkout: add a billing address (ONLY) field

Hello there,

 

I have already checked a similar question but related to the shipping address, and basically I have based what I have already done on the answer of this topic

 

Anyway, the problem is that I am able now to show a field only in the billing address form, but I am not able in any mixin to get the data from the field that I have added.

 

I will show you what I did, so maybe you can point me in the right direction.

 

I have declared a plugin for the LayoutProcessor, and written an afterProcess function that basically does this:

 

 

$customAttributeCode = "my_attribute_code";
$customAttributeLabel = "my_attribute_label";
$customField = [
            'component' => 'Magento_Ui/js/form/element/abstract',
            'config' => [
                // customScope is used to group elements within a single form (e.g. they can be validated separately)
                'customScope' => 'billingAddress.custom_attributes',
                'customEntry' => null,
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/input',
                'tooltip' => [
                    'description' => 'Desc here',
                ],
            ],
            'dataScope' => 'billingAddress.custom_attributes' . '.' . $customAttributeCode,
            'label' => $customAttributeLabel,
            'provider' => 'checkoutProvider',
            'sortOrder' => 71,
            'validation' => [
               'required-entry' => false
            ],
            'options' => [],
            'filterBy' => null,
            'customEntry' => null,
            'visible' => true,
        ];

        $jsLayout['components']
                    ['checkout']['children']
                    ['steps']['children']
                    ['billing-step']['children']
                    ['payment']['children']
                    ['afterMethods']['children']
                    ['billing-address-form']['children']
                    ['form-fields']['children']
                    [$customAttributeCode] = $customField;

        return $jsLayout;

 

As as was saying before, the field is shown correctly and the related input element has the name attribute set to 

name="customAttributes[my_attribute_code]"

 

the problem is that I have tried, through a lot of actions with the mixin, to retrieve this value (tried with create-billing-address, select-billing-address, set-billing-address) but the billingAddress object has always customAttributes = undefined.

 

Can you please give me some hints?

 

Thanks

 

3 REPLIES

Re: Checkout: add a billing address (ONLY) field

Just a little but important note: if what I am trying to do is not the right approach, I am completely available to redesign the workflow.

Re: Checkout: add a billing address (ONLY) field

To add the field to the address model on the server side, add the extension_attributes.xml file in the <YourModule_dir>/etc/ directory.

Following is a sample extension_attributes.xml:

 

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd"> <extension_attributes for="Magento\Quote\Api\Data\AddressInterface"> <attribute code="custom_field" type="string" /> </extension_attributes> </config>

Re: Checkout: add a billing address (ONLY) field

Hi,

 

thanks for your answer.

I know how to save something through extension attributes in server-side, the problem is that I have to pass the information from the client side.

The input in the form is there, but is not considered in the JS that prepares the address data to send to the server-side.

 

Lorenzo