Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
products_with_layered_navigation_swatch.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
10 
14 $installer = Bootstrap::getObjectManager()->create(\Magento\Catalog\Setup\CategorySetup::class);
15 
16 $data = [
17  'attribute_code' => 'color_swatch',
18  'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
19  'is_global' => 1,
20  'is_user_defined' => 1,
21  'frontend_input' => 'select',
22  'is_unique' => 0,
23  'is_required' => 0,
24  'is_searchable' => 1,
25  'is_visible_in_advanced_search' => 1,
26  'is_comparable' => 1,
27  'is_filterable' => 1,
28  'is_filterable_in_search' => 1,
29  'is_used_for_promo_rules' => 0,
30  'is_html_allowed_on_front' => 1,
31  'is_visible_on_front' => 1,
32  'used_in_product_listing' => 1,
33  'used_for_sort_by' => 1,
34  'frontend_label' => ['Test Swatch'],
35  'backend_type' => 'int',
36  'use_product_image_for_swatch' => 0,
37  'update_product_preview_image' => 0,
38 ];
40 
41 $data['swatch_input_type'] = 'visual';
42 $data['swatchvisual']['value'] = array_reduce(
43  range(1, $optionsPerAttribute),
44  function ($values, $index) use ($optionsPerAttribute) {
45  $values['option_' . $index] = '#'
46  . str_repeat(
47  dechex(255 * $index / $optionsPerAttribute),
48  3
49  );
50  return $values;
51  },
52  []
53 );
54 $data['optionvisual']['value'] = array_reduce(
55  range(1, $optionsPerAttribute),
56  function ($values, $index) use ($optionsPerAttribute) {
57  $values['option_' . $index] = ['option ' . $index];
58  return $values;
59  },
60  []
61 );
62 
63 $data['options']['option'] = array_reduce(
64  range(1, $optionsPerAttribute),
65  function ($values, $index) use ($optionsPerAttribute) {
66  $values[] = [
67  'label' => 'option ' . $index,
68  'value' => 'option_' . $index,
69  ];
70  return $values;
71  },
72  []
73 );
74 
75 $options = [];
76 foreach ($data['options']['option'] as $optionData) {
77  $options[] = $objectManager->get(AttributeOptionInterface::class)
78  ->setLabel($optionData['label'])
79  ->setValue($optionData['value']);
80 }
81 
83  \Magento\Catalog\Api\Data\ProductAttributeInterface::class,
84  ['data' => $data]
85 );
86 $attribute->setOptions($options);
87 $attribute->save();
88 
89 /* Assign attribute to attribute set */
90 $installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId());
91 
93 $eavConfig = Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class);
94 $eavConfig->clear();
95 
96 $attribute = $eavConfig->getAttribute('catalog_product', 'color_swatch');
97 $options = $attribute->getOptions();
98 
99 // workaround for saved attribute
100 $attribute->setDefaultValue($options[1]->getValue());
101 
102 $attribute->save();
103 $eavConfig->clear();
104 
107 $product->isObjectNew(true);
108 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
109  ->setId(10)
110  ->setAttributeSetId(4)
111  ->setName('Simple Product1')
112  ->setSku('simple1')
113  ->setTaxClassId('none')
114  ->setDescription('description')
115  ->setShortDescription('short description')
116  ->setOptionsContainer('container1')
117  ->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_IN_CART)
118  ->setPrice(10)
119  ->setWeight(1)
120  ->setMetaTitle('meta title')
121  ->setMetaKeyword('meta keyword')
122  ->setMetaDescription('meta description')
123  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
124  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
125  ->setWebsiteIds([1])
126  ->setCategoryIds([])
127  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
128  ->setSpecialPrice('5.99')
129  ->save();
130 
131 $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
132 $product->isObjectNew(true);
133 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
134  ->setId(11)
135  ->setAttributeSetId(4)
136  ->setName('Simple Product2')
137  ->setSku('simple2')
138  ->setTaxClassId('none')
139  ->setDescription('description')
140  ->setShortDescription('short description')
141  ->setOptionsContainer('container1')
142  ->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_ON_GESTURE)
143  ->setPrice(20)
144  ->setWeight(1)
145  ->setMetaTitle('meta title')
146  ->setMetaKeyword('meta keyword')
147  ->setMetaDescription('meta description')
148  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
149  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
150  ->setWebsiteIds([1])
151  ->setCategoryIds([])
152  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 50, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
153  ->setSpecialPrice('15.99')
154  ->save();
155 
156 $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
157 $product->isObjectNew(true);
158 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
159  ->setId(12)
160  ->setAttributeSetId(4)
161  ->setName('Simple Product3')
162  ->setSku('simple3')
163  ->setTaxClassId('none')
164  ->setDescription('description')
165  ->setShortDescription('short description')
166  ->setPrice(30)
167  ->setWeight(1)
168  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
169  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
170  ->setWebsiteIds([1])
171  ->setCategoryIds([])
172  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 140, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
173  ->setSpecialPrice('25.99')
174  ->save();
175 
177 $category->isObjectNew(true);
178 $category->setId(
179  333
180 )->setCreatedAt(
181  '2014-06-23 09:50:07'
182 )->setName(
183  'Category 1'
184 )->setParentId(
185  2
186 )->setPath(
187  '1/2/333'
188 )->setLevel(
189  2
190 )->setAvailableSortBy(
191  ['position', 'name']
192 )->setDefaultSortBy(
193  'name'
194 )->setIsActive(
195  true
196 )->setPosition(
197  1
198 )->setPostedProducts(
199  [10 => 10, 11 => 11, 12 => 12]
200 )->save();
201 
203 $indexerCollection = Bootstrap::getObjectManager()->get(\Magento\Indexer\Model\Indexer\Collection::class);
204 $indexerCollection->load();
206 foreach ($indexerCollection->getItems() as $indexer) {
207  $indexer->reindexAll();
208 }
foreach($data['options']['option'] as $optionData) $attribute
$optionData
$values
Definition: options.phtml:88
$index
Definition: list.phtml:44