0

I am adding two custom address attribute as mobile and country_code.

InstallData.php as below,

<?php

namespace Ocra\CustomerMobile\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;
    private $attributeSetFactory;
    private $customerSetupFactory;

    public function __construct(
        AttributeSetFactory $attributeSetFactory,
        CustomerSetupFactory $customerSetupFactory,
        EavSetupFactory $eavSetupFactory,
        Config $eavConfig
        )
    {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig       = $eavConfig;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'country_code',
            [
                'type'         => 'varchar',
                'label'        => 'Country Code',
                'input'        => 'text',
                'required'     => false,
                'visible'      => true,
                'user_defined' => true,
                'position'     => 1000,
                'system'       => 0,
            ]
        );

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'country_code')
        ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => [
                    'adminhtml_customer',
                    'adminhtml_checkout',
                    'customer_account_create',
                    'customer_account_edit',
                    'checkout_register',
                ]
        ]);
        $attribute->save();

        $customerSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'mobile',
            [
                'type'         => 'varchar',
                'label'        => 'Mobile',
                'input'        => 'text',
                'required'     => false,
                'visible'      => true,
                'user_defined' => true,
                'position'     => 1001,
                'system'       => 0,
            ]
        );

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'mobile')
        ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => [
                    'adminhtml_customer',
                    'adminhtml_checkout',
                    'customer_account_create',
                    'customer_account_edit',
                    'checkout_register',
                ]
        ]);
        $attribute->save();


        $customerSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'is_mobile_verified',
            [
                'type'         => 'int',
                'label'        => 'Is Mobile Verified?',
                'input'        => 'select',
                'source' => 'Ocra\CustomerMobile\Model\Config\Source\Option',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'required'     => false,
                'visible'      => true,
                'user_defined' => true,
                'position'     => 1002,
                'system'       => 0,
                'default' => 2
            ]
        );

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'is_mobile_verified')
        ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => [
                    'adminhtml_customer',
                    'adminhtml_checkout',
                    'customer_account_create',
                    'customer_account_edit',
                    'checkout_register',
                ]
        ]);
        $attribute->save();

        $customerSetup->addAttribute('customer_address', 'country_code', [
               'label' => 'Country Code',
               'input' => 'varchar',
               'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
               'source' => '',
               'required' => false,
               'position' => 90,
               'visible' => true,
               'system' => false,
               'is_used_in_grid' => false,
               'is_visible_in_grid' => false,
               'is_filterable_in_grid' => false,
               'is_searchable_in_grid' => false,
               'frontend_input' => 'hidden',
               'backend' => ''
           ]);

        $attribute=$customerSetup->getEavConfig()
                ->getAttribute('customer_address','country_code')                                  
                ->addData([
                        'attribute_set_id' => $attributeSetId,
                        'attribute_group_id' => $attributeGroupId,
                        'used_in_forms' => [
                           'adminhtml_customer_address',
                           'adminhtml_customer',
                           'customer_address_edit',
                           'customer_register_address',
                           'customer_address',
                        ]
                ]);
        $attribute->save();

        $customerSetup->addAttribute('customer_address', 'mobile', [
               'label' => 'Mobile',
               'input' => 'varchar',
               'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
               'source' => '',
               'required' => false,
               'position' => 90,
               'visible' => true,
               'system' => false,
               'is_used_in_grid' => false,
               'is_visible_in_grid' => false,
               'is_filterable_in_grid' => false,
               'is_searchable_in_grid' => false,
               'frontend_input' => 'hidden',
               'backend' => ''
           ]);

        $attribute=$customerSetup->getEavConfig()
                ->getAttribute('customer_address','mobile')                                  
                ->addData([
                        'attribute_set_id' => $attributeSetId,
                        'attribute_group_id' => $attributeGroupId,
                        'used_in_forms' => [
                           'adminhtml_customer_address',
                           'adminhtml_customer',
                           'customer_address_edit',
                           'customer_register_address',
                           'customer_address',
                        ]
                ]);
        $attribute->save();
    }
}

After this, if I open New Address from admin customer edit screen or new customer screen, address form is not loading instead error is created in debug.log and system.log as below,

[2021-01-13 11:55:16] main.CRITICAL: TypeError: Argument 2 passed to Magento\Framework\View\Element\UiComponentFactory::argumentsResolver() must be of the type array, null given, called in D:\wamp_new\www\magento_2_4\vendor\magento\framework\View\Element\UiComponentFactory.php on line 235 and defined in D:\wamp_new\www\magento_2_4\vendor\magento\framework\View\Element\UiComponentFactory.php:183 Stack trace: #0 D:\wamp_new\www\magento_2_4\vendor\magento\framework\View\Element\UiComponentFactory.php(235): Magento\Framework\View\Element\UiComponentFactory->argumentsResolver('country_code', NULL) #1 D:\wamp_new\www\magento_2_4\vendor\magento\module-ui\Component\Form\Field.php(85): Magento\Framework\View\Element\UiComponentFactory->create('country_code', 'varchar', Array) #2 D:\wamp_new\www\magento_2_4\vendor\magento\framework\View\Layout\Generator\UiComponent.php(164):

Error on the screen as below,

enter image description here

Where is it going wrong.

1 Answer 1

0

Magento 2 customers have different table's for Customer and Customer Shipping/billing Address. Adding the Eav attributes to the customer does not affect the addresses.

What you really want, as much as i know, is to add your attributes to the table: Customer_address_entity

You can try using this code:

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    $customerAddressEntity = $customerSetup->getEavConfig()->getEntityType('customer_address');
    $attributeSetId = $customerAddressEntity->getDefaultAttributeSetId();

    /** @var $attributeSet AttributeSet */
    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

    $customerSetup->addAttribute('customer_address', 'is_mobile_verified', [
        'type'         => 'int',
        'label'        => 'Is Mobile Verified?',
        'input'        => 'select',
        'source' => 'Ocra\CustomerMobile\Model\Config\Source\Option',
        'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
        'required'     => false,
        'visible'      => true,
        'user_defined' => true,
        'position'     => 1002,
        'system'       => 0,
        'default' => 2
    ]);

    $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'is_mobile_verified')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId
        ]);

    $attribute->save();
}
0

Your Answer

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

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