Magento 2 Add new Customer Attribute via UpgradeSchema
up vote
1
down vote
favorite
I have a module that has customer attributes added via the InstallSchema Script, however I have an UpgradeSchema script to also add more customer attributes;
Error below when running update in terminal;
PHP Fatal error: Declaration of
MyCompanyExtendedcustomerSetupUpgradeSchema::upgrade
(MagentoFrameworkSetupModuleDataSetupInterface $setup,
MagentoFrameworkSetupModuleContextInterface $context)
must be compatible with
MagentoFrameworkSetupUpgradeSchemaInterface::upgrade
(MagentoFrameworkSetupSchemaSetupInterface $setup,
MagentoFrameworkSetupModuleContextInterface $context)
in /app/code/MyCompany/Extendedcustomer/Setup/UpgradeSchema.php on line 17
Full Code:
<?php
namespace MyCompanyExtendedcustomerSetup;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupUpgradeSchemaInterface;
class UpgradeSchema implements UpgradeSchemaInterface
{
protected $customerSetupFactory;
public function upgrade(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$setup->startSetup();
if (version_compare($context->getVersion(), '1.0.1') < 0) {
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'current_device',
[
'type' => 'text',
'label' => 'Current Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 300,
'position' => 400,
'default' => '',
'system' => false,
'is_used_in_grid' => 0,
'is_visible_in_grid' => 1,
'is_filterable_in_grid' => 1,
'is_searchable_in_grid' => 1,
]
);
$Attribute = $customerSetup->getEavConfig()->getAttribute( MagentoCustomerModelCustomer::ENTITY, 'current_device');
$Attribute->setData(
'used_in_forms',
[
'adminhtml_customer',
'customer_account_edit',
'adminhtml_checkout',
'adminhtml_customer_address',
'customer_address_edit',
'customer_register_address',
]
);
$Attribute->save();
$customerSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'current_scandevice',
[
'type' => 'text',
'label' => 'Current RF Scan Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 300,
'position' => 400,
'default' => '',
'system' => false,
'is_used_in_grid' => 0,
'is_visible_in_grid' => 1,
'is_filterable_in_grid' => 1,
'is_searchable_in_grid' => 1,
]
);
$Attribute = $customerSetup->getEavConfig()->getAttribute( MagentoCustomerModelCustomer::ENTITY, 'current_scandevice');
$Attribute->setData(
'used_in_forms',
[
'adminhtml_customer',
'customer_account_edit',
'adminhtml_checkout',
'adminhtml_customer_address',
'customer_address_edit',
'customer_register_address',
]
);
$Attribute->save();
$setup->endSetup();
}
}
}
magento2 upgrade-script upgradeschema
add a comment |
up vote
1
down vote
favorite
I have a module that has customer attributes added via the InstallSchema Script, however I have an UpgradeSchema script to also add more customer attributes;
Error below when running update in terminal;
PHP Fatal error: Declaration of
MyCompanyExtendedcustomerSetupUpgradeSchema::upgrade
(MagentoFrameworkSetupModuleDataSetupInterface $setup,
MagentoFrameworkSetupModuleContextInterface $context)
must be compatible with
MagentoFrameworkSetupUpgradeSchemaInterface::upgrade
(MagentoFrameworkSetupSchemaSetupInterface $setup,
MagentoFrameworkSetupModuleContextInterface $context)
in /app/code/MyCompany/Extendedcustomer/Setup/UpgradeSchema.php on line 17
Full Code:
<?php
namespace MyCompanyExtendedcustomerSetup;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupUpgradeSchemaInterface;
class UpgradeSchema implements UpgradeSchemaInterface
{
protected $customerSetupFactory;
public function upgrade(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$setup->startSetup();
if (version_compare($context->getVersion(), '1.0.1') < 0) {
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'current_device',
[
'type' => 'text',
'label' => 'Current Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 300,
'position' => 400,
'default' => '',
'system' => false,
'is_used_in_grid' => 0,
'is_visible_in_grid' => 1,
'is_filterable_in_grid' => 1,
'is_searchable_in_grid' => 1,
]
);
$Attribute = $customerSetup->getEavConfig()->getAttribute( MagentoCustomerModelCustomer::ENTITY, 'current_device');
$Attribute->setData(
'used_in_forms',
[
'adminhtml_customer',
'customer_account_edit',
'adminhtml_checkout',
'adminhtml_customer_address',
'customer_address_edit',
'customer_register_address',
]
);
$Attribute->save();
$customerSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'current_scandevice',
[
'type' => 'text',
'label' => 'Current RF Scan Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 300,
'position' => 400,
'default' => '',
'system' => false,
'is_used_in_grid' => 0,
'is_visible_in_grid' => 1,
'is_filterable_in_grid' => 1,
'is_searchable_in_grid' => 1,
]
);
$Attribute = $customerSetup->getEavConfig()->getAttribute( MagentoCustomerModelCustomer::ENTITY, 'current_scandevice');
$Attribute->setData(
'used_in_forms',
[
'adminhtml_customer',
'customer_account_edit',
'adminhtml_checkout',
'adminhtml_customer_address',
'customer_address_edit',
'customer_register_address',
]
);
$Attribute->save();
$setup->endSetup();
}
}
}
magento2 upgrade-script upgradeschema
Refer to stackoverflow.com/a/35427023/1423608
– BENN1TH
Nov 30 at 9:08
You should try in UpgradeData, not in UpgradeSchema :)
– Himmat Paliwal
Nov 30 at 9:15
As @HimmatPaliwal said, useUpgradeData
instead ofUpgradeSchema
because adding a new attribute will insert a new entry of data in theeav_attributes
table not adding or altering the database schema
– Prince Patel
Nov 30 at 10:33
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a module that has customer attributes added via the InstallSchema Script, however I have an UpgradeSchema script to also add more customer attributes;
Error below when running update in terminal;
PHP Fatal error: Declaration of
MyCompanyExtendedcustomerSetupUpgradeSchema::upgrade
(MagentoFrameworkSetupModuleDataSetupInterface $setup,
MagentoFrameworkSetupModuleContextInterface $context)
must be compatible with
MagentoFrameworkSetupUpgradeSchemaInterface::upgrade
(MagentoFrameworkSetupSchemaSetupInterface $setup,
MagentoFrameworkSetupModuleContextInterface $context)
in /app/code/MyCompany/Extendedcustomer/Setup/UpgradeSchema.php on line 17
Full Code:
<?php
namespace MyCompanyExtendedcustomerSetup;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupUpgradeSchemaInterface;
class UpgradeSchema implements UpgradeSchemaInterface
{
protected $customerSetupFactory;
public function upgrade(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$setup->startSetup();
if (version_compare($context->getVersion(), '1.0.1') < 0) {
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'current_device',
[
'type' => 'text',
'label' => 'Current Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 300,
'position' => 400,
'default' => '',
'system' => false,
'is_used_in_grid' => 0,
'is_visible_in_grid' => 1,
'is_filterable_in_grid' => 1,
'is_searchable_in_grid' => 1,
]
);
$Attribute = $customerSetup->getEavConfig()->getAttribute( MagentoCustomerModelCustomer::ENTITY, 'current_device');
$Attribute->setData(
'used_in_forms',
[
'adminhtml_customer',
'customer_account_edit',
'adminhtml_checkout',
'adminhtml_customer_address',
'customer_address_edit',
'customer_register_address',
]
);
$Attribute->save();
$customerSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'current_scandevice',
[
'type' => 'text',
'label' => 'Current RF Scan Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 300,
'position' => 400,
'default' => '',
'system' => false,
'is_used_in_grid' => 0,
'is_visible_in_grid' => 1,
'is_filterable_in_grid' => 1,
'is_searchable_in_grid' => 1,
]
);
$Attribute = $customerSetup->getEavConfig()->getAttribute( MagentoCustomerModelCustomer::ENTITY, 'current_scandevice');
$Attribute->setData(
'used_in_forms',
[
'adminhtml_customer',
'customer_account_edit',
'adminhtml_checkout',
'adminhtml_customer_address',
'customer_address_edit',
'customer_register_address',
]
);
$Attribute->save();
$setup->endSetup();
}
}
}
magento2 upgrade-script upgradeschema
I have a module that has customer attributes added via the InstallSchema Script, however I have an UpgradeSchema script to also add more customer attributes;
Error below when running update in terminal;
PHP Fatal error: Declaration of
MyCompanyExtendedcustomerSetupUpgradeSchema::upgrade
(MagentoFrameworkSetupModuleDataSetupInterface $setup,
MagentoFrameworkSetupModuleContextInterface $context)
must be compatible with
MagentoFrameworkSetupUpgradeSchemaInterface::upgrade
(MagentoFrameworkSetupSchemaSetupInterface $setup,
MagentoFrameworkSetupModuleContextInterface $context)
in /app/code/MyCompany/Extendedcustomer/Setup/UpgradeSchema.php on line 17
Full Code:
<?php
namespace MyCompanyExtendedcustomerSetup;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupUpgradeSchemaInterface;
class UpgradeSchema implements UpgradeSchemaInterface
{
protected $customerSetupFactory;
public function upgrade(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$setup->startSetup();
if (version_compare($context->getVersion(), '1.0.1') < 0) {
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'current_device',
[
'type' => 'text',
'label' => 'Current Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 300,
'position' => 400,
'default' => '',
'system' => false,
'is_used_in_grid' => 0,
'is_visible_in_grid' => 1,
'is_filterable_in_grid' => 1,
'is_searchable_in_grid' => 1,
]
);
$Attribute = $customerSetup->getEavConfig()->getAttribute( MagentoCustomerModelCustomer::ENTITY, 'current_device');
$Attribute->setData(
'used_in_forms',
[
'adminhtml_customer',
'customer_account_edit',
'adminhtml_checkout',
'adminhtml_customer_address',
'customer_address_edit',
'customer_register_address',
]
);
$Attribute->save();
$customerSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'current_scandevice',
[
'type' => 'text',
'label' => 'Current RF Scan Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 300,
'position' => 400,
'default' => '',
'system' => false,
'is_used_in_grid' => 0,
'is_visible_in_grid' => 1,
'is_filterable_in_grid' => 1,
'is_searchable_in_grid' => 1,
]
);
$Attribute = $customerSetup->getEavConfig()->getAttribute( MagentoCustomerModelCustomer::ENTITY, 'current_scandevice');
$Attribute->setData(
'used_in_forms',
[
'adminhtml_customer',
'customer_account_edit',
'adminhtml_checkout',
'adminhtml_customer_address',
'customer_address_edit',
'customer_register_address',
]
);
$Attribute->save();
$setup->endSetup();
}
}
}
magento2 upgrade-script upgradeschema
magento2 upgrade-script upgradeschema
asked Nov 30 at 8:39
BENN1TH
5641516
5641516
Refer to stackoverflow.com/a/35427023/1423608
– BENN1TH
Nov 30 at 9:08
You should try in UpgradeData, not in UpgradeSchema :)
– Himmat Paliwal
Nov 30 at 9:15
As @HimmatPaliwal said, useUpgradeData
instead ofUpgradeSchema
because adding a new attribute will insert a new entry of data in theeav_attributes
table not adding or altering the database schema
– Prince Patel
Nov 30 at 10:33
add a comment |
Refer to stackoverflow.com/a/35427023/1423608
– BENN1TH
Nov 30 at 9:08
You should try in UpgradeData, not in UpgradeSchema :)
– Himmat Paliwal
Nov 30 at 9:15
As @HimmatPaliwal said, useUpgradeData
instead ofUpgradeSchema
because adding a new attribute will insert a new entry of data in theeav_attributes
table not adding or altering the database schema
– Prince Patel
Nov 30 at 10:33
Refer to stackoverflow.com/a/35427023/1423608
– BENN1TH
Nov 30 at 9:08
Refer to stackoverflow.com/a/35427023/1423608
– BENN1TH
Nov 30 at 9:08
You should try in UpgradeData, not in UpgradeSchema :)
– Himmat Paliwal
Nov 30 at 9:15
You should try in UpgradeData, not in UpgradeSchema :)
– Himmat Paliwal
Nov 30 at 9:15
As @HimmatPaliwal said, use
UpgradeData
instead of UpgradeSchema
because adding a new attribute will insert a new entry of data in the eav_attributes
table not adding or altering the database schema– Prince Patel
Nov 30 at 10:33
As @HimmatPaliwal said, use
UpgradeData
instead of UpgradeSchema
because adding a new attribute will insert a new entry of data in the eav_attributes
table not adding or altering the database schema– Prince Patel
Nov 30 at 10:33
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
Please try with below code if it works:
<?php
namespace MyCompanyExtendedcustomerSetup;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerModelCustomer;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupUpgradeDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
class UpgradeData implements UpgradeDataInterface
{
protected $customerSetupFactory;
private $attributeSetFactory;
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
if (version_compare($context->getVersion(), '1.0.1') < 0) {
$customerSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'current_device',
[
'type' => 'text',
'label' => 'Current Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]
);
$Attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'current_device')
->addData([
'attribute_set_id' => 1,
'attribute_group_id' => 1,
'used_in_forms' => ['adminhtml_customer','checkout_register', 'customer_account_create', 'customer_account_edit','adminhtml_checkout'],
]);
$Attribute->save();
$customerSetup->addAttribute(Customer::ENTITY, 'current_scandevice', [
'type' => 'varchar',
'label' => 'Current RF Scan Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
$Attribute2 = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'current_scandevice')
->addData([
'attribute_set_id' => 1,
'attribute_group_id' => 1,
'used_in_forms' => ['adminhtml_customer','checkout_register', 'customer_account_create', 'customer_account_edit','adminhtml_checkout'],
]);
$Attribute2->save();
}
$setup->endSetup();
}
}
Please let me know in case still it creates issue.
Thankyou @Himmat +1
– BENN1TH
Nov 30 at 9:21
Welcome anytime :)
– Himmat Paliwal
Nov 30 at 9:44
add a comment |
up vote
0
down vote
DO it by InstallData.
Way I prefere
app/code/vendor/moduel/Setup/InstallData.php
<?php
namespace Vendor/ModuelSetup;
use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoEavModelConfig;
use MagentoCustomerModelCustomer;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
{
$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'sample_attribute',
[
'type' => 'varchar',
'label' => 'Sample Attribute',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]
);
$sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute');
// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
$sampleAttribute->setData(
'used_in_forms',
['adminhtml_customer']
);
$sampleAttribute->save();
}
}
I have already run InstallSchema, module is in upgrading level
– BENN1TH
Nov 30 at 9:18
What happens when I have already run InstallData but have new attributes to add via version changes ..
– BENN1TH
Nov 30 at 9:19
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
Please try with below code if it works:
<?php
namespace MyCompanyExtendedcustomerSetup;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerModelCustomer;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupUpgradeDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
class UpgradeData implements UpgradeDataInterface
{
protected $customerSetupFactory;
private $attributeSetFactory;
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
if (version_compare($context->getVersion(), '1.0.1') < 0) {
$customerSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'current_device',
[
'type' => 'text',
'label' => 'Current Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]
);
$Attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'current_device')
->addData([
'attribute_set_id' => 1,
'attribute_group_id' => 1,
'used_in_forms' => ['adminhtml_customer','checkout_register', 'customer_account_create', 'customer_account_edit','adminhtml_checkout'],
]);
$Attribute->save();
$customerSetup->addAttribute(Customer::ENTITY, 'current_scandevice', [
'type' => 'varchar',
'label' => 'Current RF Scan Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
$Attribute2 = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'current_scandevice')
->addData([
'attribute_set_id' => 1,
'attribute_group_id' => 1,
'used_in_forms' => ['adminhtml_customer','checkout_register', 'customer_account_create', 'customer_account_edit','adminhtml_checkout'],
]);
$Attribute2->save();
}
$setup->endSetup();
}
}
Please let me know in case still it creates issue.
Thankyou @Himmat +1
– BENN1TH
Nov 30 at 9:21
Welcome anytime :)
– Himmat Paliwal
Nov 30 at 9:44
add a comment |
up vote
5
down vote
accepted
Please try with below code if it works:
<?php
namespace MyCompanyExtendedcustomerSetup;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerModelCustomer;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupUpgradeDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
class UpgradeData implements UpgradeDataInterface
{
protected $customerSetupFactory;
private $attributeSetFactory;
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
if (version_compare($context->getVersion(), '1.0.1') < 0) {
$customerSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'current_device',
[
'type' => 'text',
'label' => 'Current Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]
);
$Attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'current_device')
->addData([
'attribute_set_id' => 1,
'attribute_group_id' => 1,
'used_in_forms' => ['adminhtml_customer','checkout_register', 'customer_account_create', 'customer_account_edit','adminhtml_checkout'],
]);
$Attribute->save();
$customerSetup->addAttribute(Customer::ENTITY, 'current_scandevice', [
'type' => 'varchar',
'label' => 'Current RF Scan Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
$Attribute2 = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'current_scandevice')
->addData([
'attribute_set_id' => 1,
'attribute_group_id' => 1,
'used_in_forms' => ['adminhtml_customer','checkout_register', 'customer_account_create', 'customer_account_edit','adminhtml_checkout'],
]);
$Attribute2->save();
}
$setup->endSetup();
}
}
Please let me know in case still it creates issue.
Thankyou @Himmat +1
– BENN1TH
Nov 30 at 9:21
Welcome anytime :)
– Himmat Paliwal
Nov 30 at 9:44
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
Please try with below code if it works:
<?php
namespace MyCompanyExtendedcustomerSetup;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerModelCustomer;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupUpgradeDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
class UpgradeData implements UpgradeDataInterface
{
protected $customerSetupFactory;
private $attributeSetFactory;
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
if (version_compare($context->getVersion(), '1.0.1') < 0) {
$customerSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'current_device',
[
'type' => 'text',
'label' => 'Current Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]
);
$Attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'current_device')
->addData([
'attribute_set_id' => 1,
'attribute_group_id' => 1,
'used_in_forms' => ['adminhtml_customer','checkout_register', 'customer_account_create', 'customer_account_edit','adminhtml_checkout'],
]);
$Attribute->save();
$customerSetup->addAttribute(Customer::ENTITY, 'current_scandevice', [
'type' => 'varchar',
'label' => 'Current RF Scan Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
$Attribute2 = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'current_scandevice')
->addData([
'attribute_set_id' => 1,
'attribute_group_id' => 1,
'used_in_forms' => ['adminhtml_customer','checkout_register', 'customer_account_create', 'customer_account_edit','adminhtml_checkout'],
]);
$Attribute2->save();
}
$setup->endSetup();
}
}
Please let me know in case still it creates issue.
Please try with below code if it works:
<?php
namespace MyCompanyExtendedcustomerSetup;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerModelCustomer;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupUpgradeDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
class UpgradeData implements UpgradeDataInterface
{
protected $customerSetupFactory;
private $attributeSetFactory;
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
if (version_compare($context->getVersion(), '1.0.1') < 0) {
$customerSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'current_device',
[
'type' => 'text',
'label' => 'Current Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]
);
$Attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'current_device')
->addData([
'attribute_set_id' => 1,
'attribute_group_id' => 1,
'used_in_forms' => ['adminhtml_customer','checkout_register', 'customer_account_create', 'customer_account_edit','adminhtml_checkout'],
]);
$Attribute->save();
$customerSetup->addAttribute(Customer::ENTITY, 'current_scandevice', [
'type' => 'varchar',
'label' => 'Current RF Scan Device',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
$Attribute2 = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'current_scandevice')
->addData([
'attribute_set_id' => 1,
'attribute_group_id' => 1,
'used_in_forms' => ['adminhtml_customer','checkout_register', 'customer_account_create', 'customer_account_edit','adminhtml_checkout'],
]);
$Attribute2->save();
}
$setup->endSetup();
}
}
Please let me know in case still it creates issue.
edited Nov 30 at 9:16
answered Nov 30 at 9:08
Himmat Paliwal
1,088417
1,088417
Thankyou @Himmat +1
– BENN1TH
Nov 30 at 9:21
Welcome anytime :)
– Himmat Paliwal
Nov 30 at 9:44
add a comment |
Thankyou @Himmat +1
– BENN1TH
Nov 30 at 9:21
Welcome anytime :)
– Himmat Paliwal
Nov 30 at 9:44
Thankyou @Himmat +1
– BENN1TH
Nov 30 at 9:21
Thankyou @Himmat +1
– BENN1TH
Nov 30 at 9:21
Welcome anytime :)
– Himmat Paliwal
Nov 30 at 9:44
Welcome anytime :)
– Himmat Paliwal
Nov 30 at 9:44
add a comment |
up vote
0
down vote
DO it by InstallData.
Way I prefere
app/code/vendor/moduel/Setup/InstallData.php
<?php
namespace Vendor/ModuelSetup;
use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoEavModelConfig;
use MagentoCustomerModelCustomer;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
{
$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'sample_attribute',
[
'type' => 'varchar',
'label' => 'Sample Attribute',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]
);
$sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute');
// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
$sampleAttribute->setData(
'used_in_forms',
['adminhtml_customer']
);
$sampleAttribute->save();
}
}
I have already run InstallSchema, module is in upgrading level
– BENN1TH
Nov 30 at 9:18
What happens when I have already run InstallData but have new attributes to add via version changes ..
– BENN1TH
Nov 30 at 9:19
add a comment |
up vote
0
down vote
DO it by InstallData.
Way I prefere
app/code/vendor/moduel/Setup/InstallData.php
<?php
namespace Vendor/ModuelSetup;
use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoEavModelConfig;
use MagentoCustomerModelCustomer;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
{
$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'sample_attribute',
[
'type' => 'varchar',
'label' => 'Sample Attribute',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]
);
$sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute');
// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
$sampleAttribute->setData(
'used_in_forms',
['adminhtml_customer']
);
$sampleAttribute->save();
}
}
I have already run InstallSchema, module is in upgrading level
– BENN1TH
Nov 30 at 9:18
What happens when I have already run InstallData but have new attributes to add via version changes ..
– BENN1TH
Nov 30 at 9:19
add a comment |
up vote
0
down vote
up vote
0
down vote
DO it by InstallData.
Way I prefere
app/code/vendor/moduel/Setup/InstallData.php
<?php
namespace Vendor/ModuelSetup;
use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoEavModelConfig;
use MagentoCustomerModelCustomer;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
{
$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'sample_attribute',
[
'type' => 'varchar',
'label' => 'Sample Attribute',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]
);
$sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute');
// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
$sampleAttribute->setData(
'used_in_forms',
['adminhtml_customer']
);
$sampleAttribute->save();
}
}
DO it by InstallData.
Way I prefere
app/code/vendor/moduel/Setup/InstallData.php
<?php
namespace Vendor/ModuelSetup;
use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoEavModelConfig;
use MagentoCustomerModelCustomer;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
{
$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'sample_attribute',
[
'type' => 'varchar',
'label' => 'Sample Attribute',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]
);
$sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute');
// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
$sampleAttribute->setData(
'used_in_forms',
['adminhtml_customer']
);
$sampleAttribute->save();
}
}
answered Nov 30 at 9:13
Adarsh Shukla
8011
8011
I have already run InstallSchema, module is in upgrading level
– BENN1TH
Nov 30 at 9:18
What happens when I have already run InstallData but have new attributes to add via version changes ..
– BENN1TH
Nov 30 at 9:19
add a comment |
I have already run InstallSchema, module is in upgrading level
– BENN1TH
Nov 30 at 9:18
What happens when I have already run InstallData but have new attributes to add via version changes ..
– BENN1TH
Nov 30 at 9:19
I have already run InstallSchema, module is in upgrading level
– BENN1TH
Nov 30 at 9:18
I have already run InstallSchema, module is in upgrading level
– BENN1TH
Nov 30 at 9:18
What happens when I have already run InstallData but have new attributes to add via version changes ..
– BENN1TH
Nov 30 at 9:19
What happens when I have already run InstallData but have new attributes to add via version changes ..
– BENN1TH
Nov 30 at 9:19
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f251937%2fmagento-2-add-new-customer-attribute-via-upgradeschema%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Refer to stackoverflow.com/a/35427023/1423608
– BENN1TH
Nov 30 at 9:08
You should try in UpgradeData, not in UpgradeSchema :)
– Himmat Paliwal
Nov 30 at 9:15
As @HimmatPaliwal said, use
UpgradeData
instead ofUpgradeSchema
because adding a new attribute will insert a new entry of data in theeav_attributes
table not adding or altering the database schema– Prince Patel
Nov 30 at 10:33