create customer attribute

Notes

Custom attributes are those added on behalf of a merchant. For example, a merchant might need to add attributes to describe products, such as shape or volume. A merchant can add these attributes in the Magento Admin panel.

Custom attributes are a subset of EAV attributes.

Experiments

<?php
namespace VendorName\TestModule\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class TestPatch implements DataPatchInterface
{
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        CustomerSetupFactory $customerSetupFactory,
        EavSetupFactory $eavSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->customerSetupFactory = $customerSetupFactory;
        $this->eavSetupFactory = $eavSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }

    public function getAliases()
    {
        /**
         * This internal Magento method, that means that some patches with time can change their names,
         * but changing name should not affect installation process, that's why if we will change name of the patch
         * we will add alias here
         */
        return [];
    }

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

        $customerSetup->addAttribute(
            Customer::ENTITY,
            'c_telephone',
            [
                'type' => 'varchar',
                'label' => 'Custom telephone',
                'input' => 'text',
                'required' => false,
                'position' => 250,
                'visible' => true,
                'user_defined' => 1,
                'group' => 'General',
                'system' => false,
                'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
                'is_used_in_grid' => true,
                'is_visible_in_grid' => true,
                'is_filterable_in_grid' => true,
                'is_searchable_in_grid' => true,
            ]
        );

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'c_telephone');

        $attribute->setData(
            'used_in_forms' , ['customer_account_edit', 'customer_account_create', 'adminhtml_customer', 'adminhtml_checkout']
        );

        $attribute->save();
    }

    public static function getDependencies()
    {
        /**
         * This is dependency to another patch. Dependency should be applied first
         * One patch can have few dependencies
         * Patches do not have versions, so if in old approach with Install/Ugrade data scripts you used
         * versions, right now you need to point from patch with higher version to patch with lower version
         * But please, note, that some of your patches can be independent and can be installed in any sequence
         * So use dependencies only if this important for you
         */
        // return [
        //     SomeDependency::class
        // ];

        return [];
    }

}

Now, you can use getCustomAttributes to get it’s value.

...
    $customerResp = $this->_objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface');
    $customer = $customerResp->getById(1);
    // var_dump(get_class($customer));
    // var_dump(get_class_methods($customer));
    $customAttributes = $customer->getCustomAttributes();

    var_dump(($customAttributes));

    var_dump($customer->getExtensionAttributes());
...

Practice tests

You need to programmatically create a new customer attribute
What steps are required to do this? (Multiple Choice)
A. Save the attribute.
B. Specify the used_in_forms data for the attribute.
C. Create the attribute with \Magento\Eav\Setup\EavSetup::addAttribute
D. Set the source_model value for the attribute.
E. Add the attribute to the customer_eav_attribute table.

Answer A B C

References

How To Add Customer Attribute Programmatically

发表评论

电子邮件地址不会被公开。 必填项已用*标注