{ Hello Magento 2 }

解决 Magento 2 应用问题,更注重深度挖掘。(ง •̀_•́)ง

0%

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?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.

1
2
3
4
5
6
7
8
9
10
11
...
$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