Magento 2 Add new Customer Attribute via UpgradeSchema
-
06-02-2021 - |
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
MyCompany\Extendedcustomer\Setup\UpgradeSchema::upgrade
(Magento\Framework\Setup\ModuleDataSetupInterface $setup,
Magento\Framework\Setup\ModuleContextInterface $context)
must be compatible with
Magento\Framework\Setup\UpgradeSchemaInterface::upgrade
(Magento\Framework\Setup\SchemaSetupInterface $setup,
Magento\Framework\Setup\ModuleContextInterface $context)
in /app/code/MyCompany/Extendedcustomer/Setup/UpgradeSchema.php on line 17
Full Code:
<?php
namespace MyCompany\Extendedcustomer\Setup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;
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(
\Magento\Customer\Model\Customer::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( \Magento\Customer\Model\Customer::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(
\Magento\Customer\Model\Customer::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( \Magento\Customer\Model\Customer::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();
}
}
}
Solution
Please try with below code if it works:
<?php
namespace MyCompany\Extendedcustomer\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\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
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(
\Magento\Customer\Model\Customer::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.
OTHER TIPS
DO it by InstallData. Way I prefere app/code/vendor/moduel/Setup/InstallData.php
<?php
namespace Vendor/Moduel\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;
use Magento\Customer\Model\Customer;
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(
\Magento\Customer\Model\Customer::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();
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange