0

I am trying to add a new attribute called 'customer_type' to Customer Entity. Customer module has already been registered before.

  • I have added the lines of code to /Magento/vendor/magento/module-customer/Setup/UpgradeData.php .

  • I have run 'php bin/magento setup:upgrade'

What am i doing wrong here? Please guide me through the process

<?php


namespace Magento\Customer\Setup;

use Magento\Customer\Model\Customer;
use Magento\Directory\Model\AllowedCountries;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Encryption\Encryptor;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Setup\SetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;


class UpgradeData implements UpgradeDataInterface
{
/**
 * Customer setup factory
 *
 * @var CustomerSetupFactory
 */
protected $customerSetupFactory;

/**
 * @var IndexerRegistry
 */
protected $indexerRegistry;

/**
 * @var \Magento\Eav\Model\Config
 */
protected $eavConfig;

/**
 * @var AllowedCountries
 */
private $allowedCountriesReader;

/**
 * @var StoreManagerInterface
 */
private $storeManager;

/**
 * @param CustomerSetupFactory $customerSetupFactory
 * @param IndexerRegistry $indexerRegistry
 * @param \Magento\Eav\Model\Config $eavConfig
 */
public function __construct(
    CustomerSetupFactory $customerSetupFactory,
    IndexerRegistry $indexerRegistry,
    \Magento\Eav\Model\Config $eavConfig
) {
    $this->customerSetupFactory = $customerSetupFactory;
    $this->indexerRegistry = $indexerRegistry;
    $this->eavConfig = $eavConfig;
}

/**
 * {@inheritdoc}
 * @SuppressWarnings(PHPMD.NPathComplexity)
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
 */
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();
    /** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    $customerSetup->addAttribute(
        Customer::ENTITY,
        'customer_type',
        [
            'type' => 'varchar',
            'label' => 'Customer Type',
            'input' => 'hidden',
            'required' => false,
            'sort_order' => 100,
            'visible' => false,
            'system' => true,
        ]
    );

    if (version_compare($context->getVersion(), '2.0.6', '<')) {
        $this->upgradeVersionTwoZeroSix($customerSetup);
    }

    if (version_compare($context->getVersion(), '2.0.1', '<')) {
        $this->upgradeVersionTwoZeroOne($customerSetup);
    }

    if (version_compare($context->getVersion(), '2.0.2') < 0) {
        $this->upgradeVersionTwoZeroTwo($customerSetup);
    }

    if (version_compare($context->getVersion(), '2.0.3', '<')) {
        $this->upgradeVersionTwoZeroThree($customerSetup);
    }

    if (version_compare($context->getVersion(), '2.0.4', '<')) {
        $this->upgradeVersionTwoZeroFour($customerSetup);
    }

    if (version_compare($context->getVersion(), '2.0.5', '<')) {
        $this->upgradeVersionTwoZeroFive($customerSetup, $setup);
    }

    if (version_compare($context->getVersion(), '2.0.6', '<')) {
        $setup->getConnection()->delete(
            $setup->getTable('customer_form_attribute'),
            ['form_code = ?' => 'checkout_register']
        );
    }

    if (version_compare($context->getVersion(), '2.0.7', '<')) {
        $this->upgradeVersionTwoZeroSeven($customerSetup);
        $this->upgradeCustomerPasswordResetlinkExpirationPeriodConfig($setup);
    }

    if (version_compare($context->getVersion(), '2.0.9', '<')) {
        $setup->getConnection()->beginTransaction();

        try {
            $this->migrateStoresAllowedCountriesToWebsite($setup);
            $setup->getConnection()->commit();
        } catch (\Exception $e) {
            $setup->getConnection()->rollBack();
            throw $e;
        }
    }

    $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
    $indexer->reindexAll();
    $this->eavConfig->clear();
    $setup->endSetup();
}
/* REST OF THE CODE */
}
?>

1 Answer 1

2

Your Trying to work in vendor file that's wrong practice for Magento, please follow bellow steps to create customer attribute

Create New module inside app/code
Create New Folder => Test (Vendor)
Inside Test Folder Create "CustomAttribute"

1) Test/CustomAttribute/registration.php

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

2) Test/CustomAttribute/etc/module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Test_CustomAttribute" setup_version="0.1.1">
    </module>
</config>

3) Test/CustomAttribute/SetupInstallData.php

<?php
namespace Test\CustomAttribute\Setup;

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{

    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }


    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

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

        $customerSetup->addAttribute(Customer::ENTITY, 'customer_type', [
            'type' => 'varchar',
            'label' => 'customer_type',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'position' =>999,
            'system' => 0,
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_type')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],//you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
        ]);

        $attribute->save();
    }
}

Now Run below command to install module

Run command: php bin/magento setup:upgrade

Refernce link :

https://webkul.com/blog/how-to-create-customer-custom-attribute-in-magento-2-0/

2
  • i am working in vendor just for convenience purpose now, is there any restriction that makes it impossible to create a new attribute in vendor folder?
    – Midlaj
    Jan 20, 2018 at 6:18
  • but its not peroper way , you to update version of customer module if you want worked with vendor files , it's not advisable to worked direct in vendor files , just used above code for same you will ahicheve ur goal. Jan 20, 2018 at 6:40

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.