Cheerymagicsoftware official page


Programming is a cheery magic!


EAV (Entity-Attribute-Value) entity attributes in Magento 2.

Author Poliakov Vladimir.
In conventional systems, we’ve learned that to add new attributes in entity we follow the classical route. In the table of our entity, we just add the field with value of our new attribute, describe it in the model and use.

If we’ll go the classic way in Magento and add fields to the tables of entities, then we have a lot of work - this is use in search, display in the frontend, display in the admin backend (grids and edit mode) and so on. Accordingly, the possible bugs in the work.

The Magento 2 is designed so flexibly that the developer needs to add new attributes to the entities programmatically. But, for example, if the Product and Category entities have visual add / edit mode in the admin area, then such entity as the Customer, Customer Address … that can be done programmatically. Actually, let’s add to the Customer Address “Delivery Instruction” attribute.

Create a file structure of the module:

project structure

registration.php


        <?php
         \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'CheeryMagic_CustomerAddress',
        __DIR__
    );

composer.json


    {
        "name": "cheerymagic/module-customeraddress",
        "description": "Add attribute to Customer Address Entity",
        "license": "",
        "authors": [
            {
                "name": "CustomerAddressAttribute",
                "email": ""
            }
        ],
        "minimum-stability": "dev",
        "require": {},
        "autoload": {
            "psr-4": {
                "CheeryMagic\\CustomerAddress\\": ""
            },
            "files": [
                "registration.php"
            ]
        }
    }

module.xml

    <?xml version="1.0" ?>
    
        <module name="CheeryMagic_CustomerAddress" setup_version="1.0.0"/>
    

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\Customer\Api\Data\AddressInterface">
           <attribute code="delivery_instructions" type="string"/>
        </extension_attributes>
    </config>

InstallData.php


    <?php

    namespace CheeryMagic\CustomerAddress\Setup;

    use Magento\Framework\Setup\InstallDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
    use Magento\Customer\Setup\CustomerSetupFactory;

    class InstallData implements InstallDataInterface
    {

        private $customerSetupFactory;

        /**
         * Constructor
         *
         * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
         */
        public function __construct(
            CustomerSetupFactory $customerSetupFactory
        ) {
            $this->customerSetupFactory = $customerSetupFactory;
        }

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

            $customerSetup->addAttribute('customer_address', 'delivery_instructions', [
                'label' => 'Delivery Instructions',
                'input' => 'text',
                'type' => 'varchar',
                'source' => '',
                'required' => true,
                'position' => 400,
                'visible' => true,
                'system' => false,
                'is_used_in_grid' => false,
                'is_visible_in_grid' => false,
                'is_filterable_in_grid' => false,
                'is_searchable_in_grid' => false,
                'backend' => ''
            ]);


            $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'delivery_instructions')
                ->addData(['used_in_forms' => [
                    'adminhtml_customer_address',
                    'customer_address_edit',
                    'customer_register_address'
                ]]);
            $attribute->save();
        }
    }
    

UpgradeData.php


    <?php

    namespace CheeryMagic\CustomerAddress\Setup;

    use Magento\Framework\Setup\UpgradeDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
    use Magento\Customer\Setup\CustomerSetupFactory;

    class UpgradeData implements UpgradeDataInterface
    {

        private $customerSetupFactory;

        /**
         * Constructor
         *
         * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
         */
        public function __construct(
            CustomerSetupFactory $customerSetupFactory
        ) {
            $this->customerSetupFactory = $customerSetupFactory;
        }

        /**
         * {@inheritdoc}
         */
        public function upgrade(
            ModuleDataSetupInterface $setup,
            ModuleContextInterface $context
        )
        {
            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

            $customerSetup->addAttribute('customer_address',                'delivery_instructions', [
                'label' => 'Delivery Instructions',
                'input' => 'text',
                'type' => 'varchar',
                'source' => '',
                'required' => true,
                'position' => 400,
                'visible' => true,
                'system' => false,
                'is_used_in_grid' => false,
                'is_visible_in_grid' => false,
                'is_filterable_in_grid' => false,
                'is_searchable_in_grid' => false,
                'backend' => ''
            ]);


            $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'delivery_instructions')
                ->addData(['used_in_forms' => [
                    'adminhtml_customer_address',
                    'customer_address_edit',
                    'customer_register_address'
                ]]);
            $attribute->save();
        }
    }

Upgrade our module.xml to 1.0.1

    <?xml version="1.0" ?>
    
        <module name="CheeryMagic_CustomerAddress" setup_version="1.0.1"/>
    

Run command in console: php bin/magento setup:upgrade

Go to Admin->Customers->All Customers->edit and see next:

admin view

We’ve created new custom field “Delivery Instruction”. In fronted you may work with it programmatically.


    <?php
    $this->_address = $this->_addressRepository->getById($addressId);
    $block->getAddress()->getCustomAttribute('delivery_instructions');
    $block->getAddress()->setCustomAttribute('delivery_instructions', 'My custom Delivery Intruction');

For more information on EAV and extension attributes are available on the official page: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/attributes.html

Protected by Copyscape