0

I have follow several guide or tutorial but I can't understand how insert a custom attribute to customer general info, specifically I would to insert a second email field.

Actually I've the following structure

src/app/code/
|-- Vendor_Name
|   |-- CustomerAttribute
|   |   |-- registration.php
|   |   |-- etc
|   |   |   |-- module.xml
|   |   |   |-- extension_attributes.xml
|   |   |   |-- Setup
|   |   |   |   |-- Patch
|   |   |   |   |   |-- Data
|   |   |   |   |   |   | -- AddCustomerAccountCode.php

AddCustomerAccountCode.php contains the following code

<?php

namespace Vendor\CustomerAttribute\Setup\Patch\Data;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Customer\Setup\Patch\Data\UpdateIdentifierCustomerAttributesVisibility;

class AddCustomerAccountCode implements DataPatchInterface
{
    private $moduleDataSetup;

    private $customerSetupFactory;

    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->customerSetupFactory = $customerSetupFactory;
    }


    public function apply()
    {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $customerSetup->addAttribute(Customer::ENTITY, 'email_agent', [
            'type' => 'varchar',
            'label' => 'Email Agente',
            'input' => 'text',
            'sort_order' => 100,
            'position' => 100,
            'required' => false,
            'visible' => true,
            'system' => false
        ]);
        $customerAccountCode =$customerSetup->getEavConfig()->clear()
            ->getAttribute(Customer::ENTITY, 'email_agent');


        if ($customerAccountCode->getAttributeId()) {
            $usedInForms =  ['adminhtml_customer','customer_account_create'];
            $data = [];
            foreach ($usedInForms as $formCode) {
                $data[] = ['form_code' => $formCode, 'attribute_id' => $customerAccountCode->getAttributeId()];
            }
            $this->moduleDataSetup->getConnection()->insertMultiple(
                $this->moduleDataSetup->getTable('customer_form_attribute'),
                $data
            );
            $this->moduleDataSetup->getConnection()->endSetup();
        }
     }
    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [
            UpdateIdentifierCustomerAttributesVisibility::class,
        ];
    }

    public function getAliases()
    {
        return [];
    }
}

File module.xml into etc folder has these lines

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="VendoName_CustomerAttribute" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

Where is the trick?

Thanks

1 Answer 1

0

Here goes

composer.json

{
    "name": "xigen/module-customer",
    "description": "",
    "type": "magento2-module",
    "license": "proprietary",
    "authors": [
        {
            "name": "name",
            "email": "email@email.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {},
    "autoload": {
        "psr-4": {
            "Xigen\\Customer\\": ""
        },
        "files": [
            "registration.php"
        ]
    }
}

registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Xigen_Customer',
    __DIR__
);

etc/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\CustomerInterface">
        <attribute code="second_email" type="string"/>
    </extension_attributes>
</config>

etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Xigen_Customer" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

Setup/InstallData.php

namespace Xigen\Customer\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\Customer;
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(\Magento\Customer\Model\Customer::ENTITY, 'second_email', [
            'type' => 'varchar',
            'label' => 'Second Email',
            'input' => 'text',
            'source' => '',
            'required' => false,
            'visible' => true,
            'position' => 50,
            'system' => false,
            'backend' => ''
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'second_email')
        ->addData(['used_in_forms' => [
                'adminhtml_customer',
                'adminhtml_checkout',
                'customer_account_create',
                'customer_account_edit'
            ]
        ]);
        $attribute->save();
    }
}

Then the usual

php bin/magento setup:upgrade

php bin/magento cache:clean

2
  • Ok I get your code and at the end, before cleaning cache, I received this message on screen Nothing to import. Please re-run Magento compile command. Use the command "setup:di:compile" I need to clean cache and redo compiling of static content to manage admin page but still nothing appears on customers form Jun 6, 2019 at 8:51
  • In Magento 2 version information is stored on setup_module table. Check if there is any reference to your extension in there. Might need to use php bin/magento module:enable Vendor_Extension Jun 7, 2019 at 8:37

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.