Set custom Customer attribute value programmatically Magento 2

Set custom Customer attribute value programmatically Magento 2

July 5, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Set custom Customer attribute value programmatically Magento 2

Programmatically Setting Custom Customer Attributes in Magento 2

Magento 2 allows you to create custom attributes for your customers to store additional information. However, setting these attribute values programmatically can be tricky if not done correctly. Let’s explore how to achieve this effectively.

Understanding the Problem

In your provided code snippet, you’re using Magento’s Object Manager which is generally discouraged due to its circular dependency issues. Nevertheless, let’s dissect what might be happening here:

  1. You are successfully loading an existing customer by email.
  2. You attempt to set custom attribute values (Attr1 and Attr2) on the loaded object.
  3. The standard attributes like lastname and firstname are being saved correctly.
  4. However, the custom attributes are not saving properly.

The Correct Approach

To set custom customer attributes programmatically in Magento 2, you should follow these steps:

Step 1: Load or Create the Customer Object

$customer = $objectManager->create('\Magento\Customer\Model\Customer');

If the customer already exists by email:

$customer = $objectManager->create('Magento\Customer\Model\Customer')->loadByEmail('customrr@custom.com');

If not, create a new one:

$customer = $customerFactory->create()->setWebsiteId(1);

Step 2: Set the Custom Attribute Values

The issue with your current approach is that you’re setting the values using $customer->setAttr1(1);. While this works in some contexts, Magento’s custom customer attributes require a specific syntax.

You should use Magento\Framework\Api\DataObjectHelper to set the attribute values:

$customAttributeData = [
    'attribute_code' => 'attr1',
    'value' => 1,
];

$helper = $objectManager->create('\Magento\Framework\Api\DataObjectHelper');
$helper->populateWithArray($customer, $customAttributeData);

You repeat this process for each custom attribute you want to set.

Step 3: Save the Customer

Finally, save the customer object:

$customer->save();

Best Practices

  1. Avoid using the Object Manager directly if possible. Prefer using dependency injection or factories.
  2. Ensure your custom attributes are defined in the eav_attribute table and have the correct frontend and backend types configured.
  3. If you’re creating a new customer, make sure to set the website_id correctly.

Conclusion

Setting custom customer attribute values programmatically in Magento 2 requires understanding how Magento’s data object population works. By using Magento\Framework\Api\DataObjectHelper, you can easily populate these attributes with the desired values before saving the customer object.

Remember, proper error handling and validation should be implemented to ensure the integrity of your customer data.

For a more detailed guide on working with customers in Magento 2, I recommend checking out our blog post Extending Customer Functionality in Magento 2.

Happy coding!

Magento 2 Themes Configuration Email

Our network