-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
product_configurable.php
142 lines (118 loc) · 5.14 KB
/
product_configurable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Setup\CategorySetup;
use Magento\ConfigurableProduct\Helper\Product\Options\Factory;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Eav\Api\Data\AttributeOptionInterface;
use Magento\TestFramework\Helper\Bootstrap;
\Magento\TestFramework\Helper\Bootstrap::getInstance()->reinitialize();
require __DIR__ . '/configurable_attribute.php';
/** @var ProductRepositoryInterface $productRepository */
$productRepository = Bootstrap::getObjectManager()
->create(ProductRepositoryInterface::class);
/** @var $installer CategorySetup */
$installer = Bootstrap::getObjectManager()->create(CategorySetup::class);
/* Create simple products per each option value*/
/** @var AttributeOptionInterface[] $options */
$options = $attribute->getOptions();
$attributeValues = [];
$attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
$associatedProductIds = [];
$productIds = [10, 20];
array_shift($options); //remove the first option which is empty
foreach ($options as $option) {
/** @var $product Product */
$product = Bootstrap::getObjectManager()->create(Product::class);
$productId = array_shift($productIds);
$product->setTypeId(Type::TYPE_SIMPLE)
->setId($productId)
->setAttributeSetId($attributeSetId)
->setWebsiteIds([1])
->setName('Configurable Option' . $option->getLabel())
->setSku('simple_' . $productId)
->setPrice($productId)
->setTestConfigurable($option->getValue())
->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
->setStatus(Status::STATUS_ENABLED)
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
$product = $productRepository->save($product);
/** @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */
$stockItem = Bootstrap::getObjectManager()->create(\Magento\CatalogInventory\Model\Stock\Item::class);
$stockItem->load($productId, 'product_id');
if (!$stockItem->getProductId()) {
$stockItem->setProductId($productId);
}
$stockItem->setUseConfigManageStock(1);
$stockItem->setQty(1000);
$stockItem->setIsQtyDecimal(0);
$stockItem->setIsInStock(1);
$stockItem->save();
$attributeValues[] = [
'label' => 'test',
'attribute_id' => $attribute->getId(),
'value_index' => $option->getValue(),
];
$associatedProductIds[] = $product->getId();
}
/** @var $product Product */
$product = Bootstrap::getObjectManager()->create(Product::class);
/** @var Factory $optionsFactory */
$optionsFactory = Bootstrap::getObjectManager()->create(Factory::class);
$configurableAttributesData = [
[
'attribute_id' => $attribute->getId(),
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getStoreLabel(),
'position' => '0',
'values' => $attributeValues,
],
];
$configurableOptions = $optionsFactory->create($configurableAttributesData);
$extensionConfigurableAttributes = $product->getExtensionAttributes();
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
$product->setExtensionAttributes($extensionConfigurableAttributes);
// Remove any previously created product with the same id.
/** @var \Magento\Framework\Registry $registry */
$registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
try {
$productToDelete = $productRepository->getById(1);
$productRepository->delete($productToDelete);
/** @var \Magento\Quote\Model\ResourceModel\Quote\Item $itemResource */
$itemResource = Bootstrap::getObjectManager()->get(\Magento\Quote\Model\ResourceModel\Quote\Item::class);
$itemResource->getConnection()->delete(
$itemResource->getMainTable(),
'product_id = ' . $productToDelete->getId()
);
} catch (\Exception $e) {
// Nothing to remove
}
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
$product->setTypeId(Configurable::TYPE_CODE)
->setId(1)
->setAttributeSetId($attributeSetId)
->setWebsiteIds([1])
->setName('Configurable Product')
->setSku('configurable')
->setVisibility(Visibility::VISIBILITY_BOTH)
->setStatus(Status::STATUS_ENABLED)
->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
$productRepository->save($product);
/** @var \Magento\Catalog\Api\CategoryLinkManagementInterface $categoryLinkManagement */
$categoryLinkManagement = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->create(\Magento\Catalog\Api\CategoryLinkManagementInterface::class);
$categoryLinkManagement->assignProductToCategories(
$product->getSku(),
[2]
);