Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EavVariationsFixture.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Setup\Fixtures;
8 
10 use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
16 
21 {
25  protected $priority = 40;
26 
27  const ATTRIBUTE_SET_ID = 4;
28 
32  private $eavConfig;
33 
37  private $cache;
38 
42  private $storeManager;
43 
47  private $attributeSet;
48 
52  private $attributeFactory;
53 
63  public function __construct(
65  Config $eavConfig,
66  CacheInterface $cache,
67  StoreManager $storeManager,
68  Set $attributeSet,
69  AttributeFactory $attributeFactory
70  ) {
71  parent::__construct($fixtureModel);
72  $this->eavConfig = $eavConfig;
73  $this->cache = $cache;
74  $this->storeManager = $storeManager;
75  $this->attributeSet = $attributeSet;
76  $this->attributeFactory = $attributeFactory;
77  }
78 
82  public function execute()
83  {
84  if (!$this->fixtureModel->getValue('configurable_products', [])
85  || in_array($this->getAttributeCode(), $this->eavConfig->getEntityAttributeCodes(Product::ENTITY))) {
86  return;
87  }
88 
89  $this->generateAttribute($this->fixtureModel->getValue('configurable_products_variation', 3));
90 
91  $cacheKey = Config::ATTRIBUTES_CACHE_ID . Product::ENTITY;
92  $this->cache->remove($cacheKey);
93  }
94 
98  public function getActionTitle()
99  {
100  return 'Generating configurable EAV variations';
101  }
102 
106  public function introduceParamLabels()
107  {
108  return [];
109  }
110 
115  private function generateAttribute($optionCount)
116  {
117  $storeIds = array_keys($this->storeManager->getStores(true));
118  $options = [];
119 
120  for ($option = 1; $option <= $optionCount; $option++) {
121  $options['order']['option_' . $option] = $option;
122  $options['value']['option_' . $option] = array_fill_keys($storeIds, 'option ' . $option);
123  $options['delete']['option_' . $option] = '';
124  }
125 
126  $data = [
127  'frontend_label' => array_fill_keys($storeIds, 'configurable variations'),
128  'frontend_input' => 'select',
129  'is_required' => '0',
130  'option' => $options,
131  'default' => ['option_0'],
132  'attribute_code' => $this->getAttributeCode(),
133  'is_global' => '1',
134  'default_value_text' => '',
135  'default_value_yesno' => '0',
136  'default_value_date' => '',
137  'default_value_textarea' => '',
138  'is_unique' => '0',
139  'is_searchable' => '1',
140  'is_visible_in_advanced_search' => '0',
141  'is_comparable' => '0',
142  'is_filterable' => '1',
143  'is_filterable_in_search' => '0',
144  'is_used_for_promo_rules' => '0',
145  'is_html_allowed_on_front' => '1',
146  'is_visible_on_front' => '0',
147  'used_in_product_listing' => '0',
148  'used_for_sort_by' => '0',
149  'source_model' => null,
150  'backend_model' => null,
151  'apply_to' => [],
152  'backend_type' => 'int',
153  'entity_type_id' => 4,
154  'is_user_defined' => 1,
155  ];
156 
157  $data['swatch_input_type'] = Swatch::SWATCH_INPUT_TYPE_VISUAL;
158  $data['swatchvisual']['value'] = array_reduce(
159  range(1, $optionCount),
160  function ($values, $index) use ($optionCount) {
161  $values['option_' . $index] = '#'
162  . str_repeat(
163  dechex(255 * $index / $optionCount),
164  3
165  );
166  return $values;
167  },
168  []
169  );
170  $data['optionvisual']['value'] = array_reduce(
171  range(1, $optionCount),
172  function ($values, $index) use ($optionCount) {
173  $values['option_' . $index] = ['option ' . $index];
174  return $values;
175  },
176  []
177  );
178 
185  $model = $this->attributeFactory->create(['data' => $data]);
186  $attributeSet = $this->attributeSet->load(self::ATTRIBUTE_SET_ID);
187 
188  $model->setAttributeSetId(self::ATTRIBUTE_SET_ID);
189  $model->setAttributeGroupId($attributeSet->getDefaultGroupId(4));
190  $model->save();
191 
192  $model->setAttributeSetId(self::ATTRIBUTE_SET_ID);
193  $model->save();
194  }
195 
199  private function getAttributeCode()
200  {
201  return 'configurable_variation';
202  }
203 }
$storeManager
$values
Definition: options.phtml:88
__construct(FixtureModel $fixtureModel, Config $eavConfig, CacheInterface $cache, StoreManager $storeManager, Set $attributeSet, AttributeFactory $attributeFactory)
$index
Definition: list.phtml:44