Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
custom_attributes.php
Go to the documentation of this file.
1 <?php
8 
10 $entityType = $objectManager->create(\Magento\Eav\Model\Entity\Type::class);
11 $entityType->loadByCode('catalog_product');
13 
15 $attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class);
16 $attributeSet->load('default', 'attribute_set_name');
18 
19 $attributeGroupId = $attributeSet->getDefaultGroupId($entityType->getDefaultAttributeSetId());
20 
22  [
23  'attribute_code' => 'test_attribute',
24  'entity_type_id' => $entityTypeId,
25  'backend_type' => 'varchar',
26  'is_required' => 1,
27  'is_user_defined' => 1,
28  'is_unique' => 0,
29  'attribute_set_id' => $attributeSetId,
30  'attribute_group_id' => $attributeGroupId,
31  ],
32 ];
33 
34 foreach ($attributeData as $data) {
36  $attribute = $objectManager->create(\Magento\Eav\Model\Entity\Attribute::class);
37  $attribute->setData($data);
38  $attribute->setIsStatic(true);
39  $attribute->save();
40 }
$attributeGroupId
$attributeData
$attributeSet
$entityTypeId
$attributeSetId
$objectManager