Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeSetFixture.php
Go to the documentation of this file.
1 <?php
8 
10 
15 {
19  private $attributeRepository;
20 
24  private $attributeManagement;
25 
29  private $attributeFactory;
30 
34  private $optionFactory;
35 
39  private $attributeSetFactory;
40 
44  private $attributeGroupFactory;
45 
49  private $attributeSetManagement;
50 
54  private $attributeGroupRepository;
55 
68  public function __construct(
69  \Magento\Catalog\Api\AttributeSetManagementInterface $attributeSetManagement,
70  \Magento\Catalog\Api\ProductAttributeGroupRepositoryInterface $attributeGroupRepository,
71  \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository,
72  \Magento\Catalog\Api\ProductAttributeManagementInterface $attributeManagement,
73  \Magento\Catalog\Api\Data\ProductAttributeInterfaceFactory $attributeFactory,
74  \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory $optionFactory,
75  \Magento\Eav\Api\Data\AttributeSetInterfaceFactory $attributeSetFactory,
76  \Magento\Eav\Api\Data\AttributeGroupInterfaceFactory $attributeGroupFactory
77  ) {
78  $this->attributeRepository = $attributeRepository;
79  $this->attributeManagement = $attributeManagement;
80  $this->attributeFactory = $attributeFactory;
81  $this->optionFactory = $optionFactory;
82  $this->attributeSetFactory = $attributeSetFactory;
83  $this->attributeGroupFactory = $attributeGroupFactory;
84  $this->attributeSetManagement = $attributeSetManagement;
85  $this->attributeGroupRepository = $attributeGroupRepository;
86  }
87 
95  public function createAttributeSet(array $attributeSetData, $sortOrder = 3)
96  {
98  $attributeSet = $this->attributeSetFactory->create();
99  $attributeSet->setAttributeSetName($attributeSetData['name']);
101 
102  try {
103  $attributeSet = $this->attributeSetManagement->create($attributeSet, 4);
104  } catch (\Exception $e) {
105  return $this->getFormattedAttributeSetData($attributeSetData);
106  }
107  $attributeSetId = $attributeSet->getAttributeSetId();
108 
110  $attributeGroup = $this->attributeGroupFactory->create();
111  $attributeGroup->setAttributeGroupName($attributeSet->getAttributeSetName() . ' - Group');
112  $attributeGroup->setAttributeSetId($attributeSetId);
113  $this->attributeGroupRepository->save($attributeGroup);
114  $attributeGroupId = $attributeGroup->getAttributeGroupId();
115 
116  $attributesData = array_key_exists(0, $attributeSetData['attributes']['attribute'])
117  ? $attributeSetData['attributes']['attribute'] : [$attributeSetData['attributes']['attribute']];
118  foreach ($attributesData as $attributeData) {
119  //Create Attribute
120  $optionsData = array_key_exists(0, $attributeData['options']['option'])
121  ? $attributeData['options']['option'] : [$attributeData['options']['option']];
122  $options = [];
123  foreach ($optionsData as $optionData) {
124  $option = $this->optionFactory->create(['data' => $optionData]);
125  $options[] = $option;
126  }
127 
129  $attribute = $this->attributeFactory->create(['data' => $attributeData]);
130  $attribute->setOptions($options);
131  $attribute->setNote('auto');
132 
133  $productAttribute = $this->attributeRepository->save($attribute);
134  $attributeId = $productAttribute->getAttributeId();
135 
136  //Associate Attribute to Attribute Set
137  $this->attributeManagement->assign($attributeSetId, $attributeGroupId, $attributeId, $sortOrder);
138  }
139 
140  return $this->getFormattedAttributeSetData($attributeSetData);
141  }
142 
149  private function getFormattedAttributeSetData($attributeSetData)
150  {
151  $attributesData = array_key_exists(0, $attributeSetData['attributes']['attribute'])
152  ? $attributeSetData['attributes']['attribute'] : [$attributeSetData['attributes']['attribute']];
153  $attributes = [];
154  foreach ($attributesData as $attributeData) {
155  $optionsData = array_key_exists(0, $attributeData['options']['option'])
156  ? $attributeData['options']['option'] : [$attributeData['options']['option']];
157  $optionsData = array_map(function ($option) {
158  return $option['label'];
159  }, $optionsData);
160  $attributes[] = [
161  'name' => $attributeData['attribute_code'],
162  'values' => $optionsData
163  ];
164  }
165 
166  return [
167  'name' => $attributeSetData['name'],
168  'attributes' => $attributes
169  ];
170  }
171 }
__construct(\Magento\Catalog\Api\AttributeSetManagementInterface $attributeSetManagement, \Magento\Catalog\Api\ProductAttributeGroupRepositoryInterface $attributeGroupRepository, \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository, \Magento\Catalog\Api\ProductAttributeManagementInterface $attributeManagement, \Magento\Catalog\Api\Data\ProductAttributeInterfaceFactory $attributeFactory, \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory $optionFactory, \Magento\Eav\Api\Data\AttributeSetInterfaceFactory $attributeSetFactory, \Magento\Eav\Api\Data\AttributeGroupInterfaceFactory $attributeGroupFactory)
$attributeSetManagement
$attributeGroupId
$optionData
$attributesData
$attributes
Definition: matrix.phtml:13