#StackBounty: #magento2 #customer-attribute #savedata How to save a dynamic customer attributes in checkout register page in magento 2?

Bounty: 50

app/code/ Cm/CustomerAttribute/Model/Plugin/Checkout/LayoutProcessor.php

    <?php
namespace CmCustomerAttributeModelPluginCheckout;

class LayoutProcessor
{
    /**
     * @param MagentoCheckoutBlockCheckoutLayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
     public function __construct(CmCustomerAttributeHelperCustomerattribute $helper)
    {
        $this->helper = $helper;
    }

    public function afterProcess(
        MagentoCheckoutBlockCheckoutLayoutProcessor $subject,
        array  $jsLayout
    ) {

 $attributeCollection = $this->helper->getUserDefinedAttribures();

    if ($attributeCollection->getSize() > 0) 
    {
         foreach ($attributeCollection as $attribute)

       {
        if ($this->helper->isAttribureForCheckoutRegister($attribute->getAttributeCode()))
        {
        $frontEndLabel = $attribute->getStoreLabel($this->helper->getStoreId());
        $attributeCode = $attribute->getAttributeCode();
        $getAttributeCodeFunction = "get" .  str_replace(' ', '', ucwords(str_replace('_', ' ', $attribute->getAttributeCode

())));
        $fieldRequiredClass = ($attribute->getIsRequired()) ? 'true' : '' ;
        if($fieldRequiredClass == '')
        {
            $fieldRequiredClass = false;
        }
        $fieldFrontendClass = ($attribute->getFrontendClass()) ? $attribute->getFrontendClass() : '';
        $fieldInput =$attribute->getFrontendInput();
        if($fieldInput == 'text')
        {
            $fieldInputType = 'input';
            $fieldAbstract =  'abstract';
            $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['shipping-address-fieldset']['children'][$attributeCode] = [
            'component' => 'Magento_Ui/js/form/element/'.$fieldAbstract.'',
            'config' => [
                'customScope' => 'shippingAddress.custom_attributes',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/'.$fieldInputType.'',
                'id' => $attributeCode
            ],
            'dataScope' => 'shippingAddress.custom_attributes.'.$attributeCode.'',
            'label' => $frontEndLabel,
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' =>[$fieldFrontendClass => true, 
                'required-entry' => $fieldRequiredClass],
            'sortOrder' => 250,
            'id' => $attributeCode
        ];
        }
        elseif ($fieldInput == 'date') {
           $fieldInputType = 'date'; 
           $fieldAbstract =  'date';
           $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['shipping-address-fieldset']['children'][$attributeCode] = [
            'component' => 'Magento_Ui/js/form/element/'.$fieldAbstract.'',
            'config' => [
                'customScope' => 'shippingAddress.custom_attributes',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/'.$fieldInputType.'',
                'id' => $attributeCode
            ],
            'dataScope' => 'shippingAddress.custom_attributes.'.$attributeCode.'',
            'label' => $frontEndLabel,
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' =>[$fieldFrontendClass => true , 
                'required-entry' => $fieldRequiredClass],
            'sortOrder' => 250,

            'id' => $attributeCode
        ];
        }
        elseif ($fieldInput == 'select') {
           $fieldInputType = 'select'; 
           $fieldAbstract =  'select';

        $attributeId = $attribute->getAttributeId();
        $objectManager = MagentoFrameworkAppObjectManager::getInstance();

        $attributeOptionAll = $objectManager->create(MagentoEavModelResourceModelEntityAttributeOptionCollection::class)
                                    ->setPositionOrder('asc')
                                    ->setAttributeFilter($attributeId)
                                    ->setStoreFilter()
                                    ->load();
        $opt_val = array();
        $allOptions=array();


        foreach ($attributeOptionAll->getData() as $key => $v) 
        {
          // $allid = $v['attribute_id'];
          $opt_val['value'] = $v['option_id'];
          $opt_val['label'] = $v['value'];
          $allOptions[] = $opt_val;
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['shipping-address-fieldset']['children'][$attributeCode] = [
            'component' => 'Magento_Ui/js/form/element/'.$fieldAbstract.'',
            'config' => [
                'customScope' => 'shippingAddress.custom_attributes',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/'.$fieldInputType.'',
                'id' => $attributeCode
            ],
            'dataScope' => 'shippingAddress.custom_attributes.'.$attributeCode.'',
            'label' => $frontEndLabel,
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' =>[$fieldFrontendClass => true , 
                'required-entry' => $fieldRequiredClass],
            'sortOrder' => 250,
             'options' =>  $allOptions,  
            'id' => $attributeCode
        ];

        }
        }
        elseif ($fieldInput == 'multiselect') {
           $fieldInputType = 'input'; 
           $fieldAbstract =  'abstract';

        $attributeId = $attribute->getAttributeId();
        $objectManager = MagentoFrameworkAppObjectManager::getInstance();

        $attributeOptionAll = $objectManager->create(MagentoEavModelResourceModelEntityAttributeOptionCollection::class)
                                    ->setPositionOrder('asc')
                                    ->setAttributeFilter($attributeId)
                                    ->setStoreFilter()
                                    ->load();
        $opt_val = array();
        $allOptions=array();


        foreach ($attributeOptionAll->getData() as $key => $v) 
        {
          // $allid = $v['attribute_id'];
          $opt_val['value'] = $v['option_id'];
          $opt_val['label'] = $v['value'];
          $allOptions[] = $opt_val;
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['shipping-address-fieldset']['children'][$attributeCode] = [
            'component' => 'Magento_Ui/js/form/element/'.$fieldAbstract.'',
            'config' => [
                'customScope' => 'shippingAddress.custom_attributes',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/'.$fieldInputType.'',
                'id' => $attributeCode
            ],
            'dataScope' => 'shippingAddress.custom_attributes.'.$attributeCode.'',
            'label' => $frontEndLabel,
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' =>[$fieldFrontendClass => true , 
                'required-entry' => $fieldRequiredClass],
            'sortOrder' => 250,
             // 'options' =>  $allOptions,  
            'id' => $attributeCode
        ];

        }
        }
        elseif ($fieldInput == 'boolean') {
           $fieldInputType = 'select'; 
           $fieldAbstract =  'select';
           $allOptions = [
                 ['value' => '0', 'label' => 'No'],
                 ['value' => '1', 'label' => 'Yes']
               ];
           $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['shipping-address-fieldset']['children'][$attributeCode] = [
            'component' => 'Magento_Ui/js/form/element/'.$fieldAbstract.'',
            'config' => [
                'customScope' => 'shippingAddress.custom_attributes',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/'.$fieldInputType.'',
                'id' => $attributeCode
            ],
            'dataScope' => 'shippingAddress.custom_attributes.'.$attributeCode.'',
            'label' => $frontEndLabel,
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' =>[$fieldFrontendClass => true, 
                'required-entry' => $fieldRequiredClass],
            'sortOrder' => 250,
             'options' =>  $allOptions,  
            'id' => $attributeCode
        ];

        }
         elseif ($fieldInput == 'textarea') {
           $fieldInputType = 'textarea'; 
           $fieldAbstract =  'abstract';
           $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['shipping-address-fieldset']['children'][$attributeCode] = [
            'component' => 'Magento_Ui/js/form/element/'.$fieldAbstract.'',
            'config' => [
                'customScope' => 'shippingAddress.custom_attributes',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/'.$fieldInputType.'',
                'id' => $attributeCode
            ],
            'dataScope' => 'shippingAddress.custom_attributes.'.$attributeCode.'',
            'label' => $frontEndLabel,
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' =>[$fieldFrontendClass => true, 
                'required-entry' => $fieldRequiredClass],
            'sortOrder' => 250,
            'id' => $attributeCode
        ];

        }



       }
      }
     }
     return $jsLayout;  
    }


}

image:

enter image description here


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.