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 */
}
?>