I'm new to Magento 2 and trying to add a customer attribute using a Data Patch.
I created the following patch class:
What I Tried:
Using addData(['used_in_forms' => ['adminhtml_customer']]).
Clearing cache and generated code.
Ensuring the attribute is not already in the database.
Converting the attribute to a Customer\Model\Attribute object.
<?php
namespace Vendor\Module\Setup\Patch\Data;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class CreateQuickbooksInvoiceAttribute implements DataPatchInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* Constructor
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* Apply patch
*/
public function apply()
{
/** @var \Magento\Customer\Setup\CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
if (!$customerSetup->getAttributeId(Customer::ENTITY, 'quickbooks_invoice')) {
// Add attribute
$customerSetup->addAttribute(Customer::ENTITY, 'quickbooks_invoice', [
'type' => 'int',
'label' => 'QuickBooks Invoice',
'input' => 'select',
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
'required' => false,
'visible' => true,
'user_defined' => true,
'system' => 0,
'position' => 100,
'sort_order' => 100,
'default' => 0,
]);
}
// Load as customer attribute to safely assign used_in_forms
/** @var \Magento\Customer\Model\Attribute $attribute */
$attribute = \Magento\Framework\App\ObjectManager::getInstance()
->create(\Magento\Customer\Model\Attribute::class)
->loadByCode(Customer::ENTITY, 'quickbooks_invoice');
$attribute->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],
]);
$attribute->save();
}
public static function getDependencies()
{
return [];
}
public function getAliases()
{
return [];
}
}
miken32
42.5k16 gold badges127 silver badges177 bronze badges
-
You'll probably do better asking on magento.stackexchange.comTangentially Perpendicular– Tangentially Perpendicular2025年09月12日 06:04:03 +00:00Commented Sep 12, 2025 at 6:04
-
This question is similar to: PHP Notice: Array to string conversion Error. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.miken32– miken322025年09月15日 22:32:09 +00:00Commented Sep 15, 2025 at 22:32
1 Answer 1
You can use :
$attribute->setData(
'used_in_forms',
['adminhtml_customer']
);
Sign up to request clarification or add additional context in comments.
1 Comment
Cihangir Yaman
Welcome to the community! Your answer looks helpful but If you could explain the causes of the problem and its solution in a little more detailed, the person asking the question could learn new information from you in the process.
lang-php