Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
attribute_set_with_image_attribute.php
Go to the documentation of this file.
1 <?php
9 
11 $attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class);
12 
13 $entityType = $objectManager->create(\Magento\Eav\Model\Entity\Type::class)->loadByCode('catalog_product');
14 $defaultSetId = $objectManager->create(\Magento\Catalog\Model\Product::class)->getDefaultAttributeSetid();
15 
16 $data = [
17  'attribute_set_name' => 'attribute_set_with_media_attribute',
18  'entity_type_id' => $entityType->getId(),
19  'sort_order' => 200,
20 ];
21 
22 $attributeSet->setData($data);
23 $attributeSet->validate();
24 $attributeSet->save();
25 $attributeSet->initFromSkeleton($defaultSetId);
26 $attributeSet->save();
27 
29  'entity_type_id' => $entityType->getId(),
30  'attribute_code' => 'funny_image',
31  'frontend_input' => 'media_image',
32  'frontend_label' => 'Funny image',
33  'backend_type' => 'varchar',
34  'is_required' => 0,
35  'is_user_defined' => 1,
36  'attribute_set_id' => $attributeSet->getId(),
37  'attribute_group_id' => $attributeSet->getDefaultGroupId(),
38 ];
39 
41 $attribute = $objectManager->create(\Magento\Catalog\Model\Entity\Attribute::class);
42 $attribute->setData($attributeData);
43 $attribute->save();