|
6 | 6 | declare(strict_types=1);
|
7 | 7 |
|
8 | 8 | use Magento\Catalog\Api\Data\CategoryInterfaceFactory;
|
| 9 | +use Magento\Catalog\Api\Data\ProductAttributeInterface; |
9 | 10 | use Magento\Catalog\Api\Data\ProductAttributeInterfaceFactory;
|
10 | 11 | use Magento\Catalog\Api\Data\ProductInterfaceFactory;
|
11 | 12 | use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
|
|
18 | 19 | use Magento\Eav\Setup\EavSetup;
|
19 | 20 | use Magento\Indexer\Model\Indexer;
|
20 | 21 | use Magento\Indexer\Model\Indexer\Collection;
|
21 |
| -use Magento\Msrp\Model\Product\Attribute\Source\Type as SourceType; |
22 | 22 | use Magento\Store\Api\WebsiteRepositoryInterface;
|
23 | 23 | use Magento\TestFramework\Helper\Bootstrap;
|
24 | 24 | use Magento\TestFramework\Helper\CacheCleaner;
|
|
27 | 27 |
|
28 | 28 | /** @var Config $eavConfig */
|
29 | 29 | $eavConfig = $objectManager->get(Config::class);
|
30 |
| - |
31 | 30 | /** @var ProductAttributeRepositoryInterface $attributeRepository */
|
32 | 31 | $attributeRepository = $objectManager->get(ProductAttributeRepositoryInterface::class);
|
33 | 32 | /** @var ProductAttributeInterfaceFactory $attributeFactory */
|
|
42 | 41 | $websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
|
43 | 42 | $baseWebsite = $websiteRepository->get('base');
|
44 | 43 |
|
| 44 | +$attributes = []; |
45 | 45 | for ($i = 1; $i <= 2; $i++) {
|
| 46 | + $attributeCode = 'test_attribute_' . $i; |
46 | 47 | $attributeModel = $attributeFactory->create();
|
47 | 48 | $attributeModel->setData(
|
48 | 49 | [
|
49 |
| - 'attribute_code' => 'test_attribute_' . $i, |
| 50 | + 'attribute_code' => $attributeCode, |
50 | 51 | 'entity_type_id' => $installer->getEntityTypeId(Product::ENTITY),
|
51 | 52 | 'is_global' => 1,
|
52 | 53 | 'is_user_defined' => 1,
|
|
66 | 67 | 'frontend_label' => ['Test Attribute ' . $i],
|
67 | 68 | 'backend_type' => 'int',
|
68 | 69 | 'option' => [
|
69 |
| - 'value' => ['option_0' => ['Option 1'], 'option_1' => ['Option 2']], |
70 |
| - 'order' => ['option_0' => 1, 'option_1' => 2], |
| 70 | + 'value' => ['option_1' => ['Option 1'], 'option_2' => ['Option 2'], 'option_3' => ['Option 3']], |
| 71 | + 'order' => ['option_1' => 1, 'option_2' => 2, 'option_3' => 3], |
71 | 72 | ],
|
72 |
| - 'default' => ['option_0'], |
73 | 73 | 'position' => 3 - $i
|
74 | 74 | ]
|
75 | 75 | );
|
76 | 76 | $attribute = $attributeRepository->save($attributeModel);
|
77 | 77 | $installer->addAttributeToGroup(Product::ENTITY, $attributeSetId, $groupId, $attribute->getId());
|
| 78 | + $attributes[$attributeCode] = $attribute; |
78 | 79 | }
|
79 | 80 |
|
| 81 | +/** @var ProductAttributeInterface $attribute1 */ |
| 82 | +$attribute1 = $attributes['test_attribute_1']; |
| 83 | +/** @var ProductAttributeInterface $attribute2 */ |
| 84 | +$attribute2 = $attributes['test_attribute_2']; |
| 85 | + |
80 | 86 | CacheCleaner::cleanAll();
|
81 | 87 | $eavConfig->clear();
|
82 | 88 |
|
83 |
| -/** @var ProductRepositoryInterface $productRepository */ |
84 |
| -$productRepository = $objectManager->get(ProductRepositoryInterface::class); |
85 |
| -/** @var ProductInterfaceFactory $productInterfaceFactory */ |
86 |
| -$productInterfaceFactory = $objectManager->get(ProductInterfaceFactory::class); |
87 |
| - |
88 |
| -/** @var Product $product */ |
89 |
| -$product = $productInterfaceFactory->create(); |
90 |
| -$product->setTypeId(Type::TYPE_SIMPLE) |
91 |
| - ->setAttributeSetId($product->getDefaultAttributeSetId()) |
92 |
| - ->setName('Simple Product1') |
93 |
| - ->setSku('simple1') |
94 |
| - ->setTaxClassId('none') |
95 |
| - ->setDescription('description') |
96 |
| - ->setShortDescription('short description') |
97 |
| - ->setOptionsContainer('container1') |
98 |
| - ->setMsrpDisplayActualPriceType(SourceType::TYPE_IN_CART) |
99 |
| - ->setPrice(10) |
100 |
| - ->setWeight(1) |
101 |
| - ->setMetaTitle('meta title') |
102 |
| - ->setMetaKeyword('meta keyword') |
103 |
| - ->setMetaDescription('meta description') |
104 |
| - ->setVisibility(Visibility::VISIBILITY_BOTH) |
105 |
| - ->setStatus(Status::STATUS_ENABLED) |
106 |
| - ->setWebsiteIds([$baseWebsite->getId()]) |
107 |
| - ->setCategoryIds([]) |
108 |
| - ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]) |
109 |
| - ->setSpecialPrice('5.99'); |
110 |
| -$simple1 = $productRepository->save($product); |
111 |
| - |
112 |
| -/** @var Product $product */ |
113 |
| -$product = $productInterfaceFactory->create(); |
114 |
| -$product->setTypeId(Type::TYPE_SIMPLE) |
115 |
| - ->setAttributeSetId($product->getDefaultAttributeSetId()) |
116 |
| - ->setName('Simple Product2') |
117 |
| - ->setSku('simple2') |
118 |
| - ->setTaxClassId('none') |
119 |
| - ->setDescription('description') |
120 |
| - ->setShortDescription('short description') |
121 |
| - ->setOptionsContainer('container1') |
122 |
| - ->setMsrpDisplayActualPriceType(SourceType::TYPE_ON_GESTURE) |
123 |
| - ->setPrice(20) |
124 |
| - ->setWeight(1) |
125 |
| - ->setMetaTitle('meta title') |
126 |
| - ->setMetaKeyword('meta keyword') |
127 |
| - ->setMetaDescription('meta description') |
128 |
| - ->setVisibility(Visibility::VISIBILITY_BOTH) |
129 |
| - ->setStatus(Status::STATUS_ENABLED) |
130 |
| - ->setWebsiteIds([$baseWebsite->getId()]) |
131 |
| - ->setCategoryIds([]) |
132 |
| - ->setStockData(['use_config_manage_stock' => 1, 'qty' => 50, 'is_qty_decimal' => 0, 'is_in_stock' => 1]) |
133 |
| - ->setSpecialPrice('15.99'); |
134 |
| -$simple2 = $productRepository->save($product); |
135 |
| - |
136 | 89 | /** @var CategoryInterfaceFactory $categoryInterfaceFactory */
|
137 | 90 | $categoryInterfaceFactory = $objectManager->get(CategoryInterfaceFactory::class);
|
138 | 91 |
|
| 92 | +/** @var Magento\Catalog\Model\Category $category */ |
139 | 93 | $category = $categoryInterfaceFactory->create();
|
140 | 94 | $category->isObjectNew(true);
|
141 | 95 | $category->setId(3334)
|
142 |
| - ->setCreatedAt('2014-06-23 09:50:07') |
143 | 96 | ->setName('Category 1')
|
144 | 97 | ->setParentId(2)
|
145 |
| - ->setPath('1/2/333') |
| 98 | + ->setPath('1/2/3334') |
146 | 99 | ->setLevel(2)
|
147 | 100 | ->setAvailableSortBy(['position', 'name'])
|
148 | 101 | ->setDefaultSortBy('name')
|
149 | 102 | ->setIsActive(true)
|
150 |
| - ->setPosition(1) |
151 |
| - ->setPostedProducts( |
152 |
| - [ |
153 |
| - $simple1->getId() => 10, |
154 |
| - $simple2->getId() => 11 |
155 |
| - ] |
156 |
| - ); |
| 103 | + ->setPosition(1); |
157 | 104 | $category->save();
|
158 | 105 |
|
| 106 | +/** @var ProductRepositoryInterface $productRepository */ |
| 107 | +$productRepository = $objectManager->get(ProductRepositoryInterface::class); |
| 108 | +/** @var ProductInterfaceFactory $productInterfaceFactory */ |
| 109 | +$productInterfaceFactory = $objectManager->get(ProductInterfaceFactory::class); |
| 110 | +$products = []; |
| 111 | +for ($i = 1; $i <= 6; $i++) { |
| 112 | + $sku = 'simple' . $i; |
| 113 | + /** @var Product $product */ |
| 114 | + $product = $productInterfaceFactory->create(); |
| 115 | + $product->setTypeId(Type::TYPE_SIMPLE) |
| 116 | + ->setAttributeSetId($product->getDefaultAttributeSetId()) |
| 117 | + ->setName('Simple Product ' . $i) |
| 118 | + ->setSku($sku) |
| 119 | + ->setUrlKey('simple-product-' . $i) |
| 120 | + ->setTaxClassId('none') |
| 121 | + ->setDescription('description') |
| 122 | + ->setShortDescription('short description') |
| 123 | + ->setPrice($i * 10) |
| 124 | + ->setWeight(1) |
| 125 | + ->setMetaTitle('meta title') |
| 126 | + ->setMetaKeyword('meta keyword') |
| 127 | + ->setMetaDescription('meta description') |
| 128 | + ->setVisibility(Visibility::VISIBILITY_BOTH) |
| 129 | + ->setStatus(Status::STATUS_ENABLED) |
| 130 | + ->setWebsiteIds([$baseWebsite->getId()]) |
| 131 | + ->setCategoryIds([$category->getId()]) |
| 132 | + ->setStockData(['use_config_manage_stock' => 1, 'qty' => 50, 'is_qty_decimal' => 0, 'is_in_stock' => 1]); |
| 133 | + $product->setData($attribute1->getAttributeCode(), getAttributeOptionValue($attribute1, 'Option 1')); |
| 134 | + $optionForSecondAttribute = 'Option ' . ($i === 1 ? 1 : ($i <= 3 ? 2 : 3)); |
| 135 | + $product->setData($attribute2->getAttributeCode(), getAttributeOptionValue($attribute2, $optionForSecondAttribute)); |
| 136 | + |
| 137 | + $products[$sku] = $productRepository->save($product); |
| 138 | +} |
| 139 | + |
159 | 140 | /** @var Collection $indexerCollection */
|
160 | 141 | $indexerCollection = $objectManager->get(Collection::class);
|
161 | 142 | $indexerCollection->load();
|
162 | 143 | /** @var Indexer $indexer */
|
163 | 144 | foreach ($indexerCollection->getItems() as $indexer) {
|
164 | 145 | $indexer->reindexAll();
|
165 | 146 | }
|
| 147 | + |
| 148 | +/** |
| 149 | + * @param ProductAttributeInterface $attribute |
| 150 | + * @param string $label |
| 151 | + * @return int|null |
| 152 | + */ |
| 153 | +function getAttributeOptionValue(ProductAttributeInterface $attribute, string $label): ?int |
| 154 | +{ |
| 155 | + foreach ($attribute->getOptions() as $option) { |
| 156 | + if ($option->getLabel() === $label) { |
| 157 | + return (int)$option->getValue(); |
| 158 | + } |
| 159 | + } |
| 160 | + return null; |
| 161 | +} |
0 commit comments