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();

}

}
}









share|improve this question






















  • 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 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















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();

}

}
}









share|improve this question






















  • 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 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













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();

}

}
}









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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, 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


















  • 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 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
















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










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.






share|improve this answer























  • Thankyou @Himmat +1
    – BENN1TH
    Nov 30 at 9:21










  • Welcome anytime :)
    – Himmat Paliwal
    Nov 30 at 9:44


















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();
}
}





share|improve this answer





















  • 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











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.






share|improve this answer























  • Thankyou @Himmat +1
    – BENN1TH
    Nov 30 at 9:21










  • Welcome anytime :)
    – Himmat Paliwal
    Nov 30 at 9:44















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.






share|improve this answer























  • Thankyou @Himmat +1
    – BENN1TH
    Nov 30 at 9:21










  • Welcome anytime :)
    – Himmat Paliwal
    Nov 30 at 9:44













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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












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();
}
}





share|improve this answer





















  • 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















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();
}
}





share|improve this answer





















  • 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













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();
}
}





share|improve this answer












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();
}
}






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents