Custom attributes with and without getCustomAttributes() method in Magento 2

Custom Attributes are the attributes that we can add with customized sources of available data types. In short, we can add Custom Attributes not available in built-in Magento. The Customer and Catalog entities use EAV entities and CustomAttributesDataInterface defines getCustomAttributes() and setCustomAttributes() methods.

In this blog, we are getting an idea of how we can get and set custom attribute values with and without getCustomAttributes() and setCustomAttributes() methods.

The getCustomAttributes() method will return only those attributes that have a ‘system’ value equal to false.

With getCustomAttributes() and setCustomAttributes() methods

For example, you need to add a boolean type field in the admin customer create and update form, you can add a custom attribute using data patch. In this example, we will define the ‘system’ value as false.

Data Patch: <Namespace>/<ModuleName>/Setup/Patch/Data/ActivateCustomerAttribute.php

public function apply()
{
    $this->moduleDataSetup->getConnection()->startSetup();

    $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);

    // Add custom attribute
    $customerSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'activated',
        [
            'type' => 'int',
            'label' => 'Activated',
            'input' => 'boolean',
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'required' => false,
            'visible' => true,
            'position' => 500,
            'system' => false,
            'is_used_in_grid' => true,
            'is_visible_in_grid' => true
        ]
    );

    // Add custom attribute in admin customer create and update form
    $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'activated')->addData([
        'used_in_forms' => [
            'adminhtml_customer',
            'customer_account_edit',
            'customer_account_create'
        ]
    ]);
    $attribute->save();


    $this->moduleDataSetup->getConnection()->endSetup();
}

Save custom attribute value:

/** @var \Magento\Customer\Model\CustomerFactory $customerFactory */
$customer = $this->customerFactory->create()->load($customerId);
$customer->setCustomAttributes('activated', true);
$customer->save();

Get custom attribute value:

/** @var \Magento\Customer\Model\CustomerFactory $customerFactory */
$customer = $this->customerFactory->create()->load($customerId);
$isActivated = $customer->getCustomAttributes('activated');

Without using getCustomAttributes() and setCustomAttributes() methods

For example, you need to add a hidden input type to count the number of failed login attempts, you can add a custom attribute using a data patch with a ‘system’ value equal to true, so we can get and set that custom attribute like other built-in customer attributes.

Data Patch: <Namespace>/<ModuleName>/Setup/Patch/Data/AddPasswordFailureCountAttribute.php

public function apply()
{
    $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
    $customerSetup->addAttribute(
        Customer::ENTITY,
        'password_failure_count',
        [
            'type' => 'static',
            'label' => 'Failures Number',
            'input' => 'hidden',
            'required' => false,
            'sort_order' => 100,
            'visible' => false,
            'system' => true,
        ]
    );
    return $this;
}

We need to add the same custom attribute in the customer_entity table because the type of attribute is static. All static type attributes are reside in the respective entity table, like customer_entity, customer_address_entity, catalog_product_entity, and catalog_catalog_entity.

DB Schema: <Namespace>/<ModuleName>/etc/db_schema.xml

<?xml version="1.0"?>

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="customer_entity">
        <column xsi:type="smallint" name="password_failure_count" unsigned="false" nullable="true" identity="false" default="0" comment="Failure Number"/>
    </table>
</schema>

Save custom attribute value:

/** @var \Magento\Customer\Model\CustomerFactory $customerFactory */
$customer = $this->customerFactory->create()->load($customerId);
$customer->setPasswordFailureCount(5);
$customer->save();

Get custom attribute value:

/** @var \Magento\Customer\Model\CustomerFactory $customerFactory */
$customer = $this->customerFactory->create()->load($customerId);
$failureCount = $customer->getPasswordFailureCount();

We hope this blog may understandable and useful to you. You can email us at mage2developer@gmail.com if we missed anything or want to add any suggestions. We will respond to you as soon as possible. Happy to help 🙂

Need Further Help? or Questions?

Contact Us

Create your account

chatsimple