Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_configurable_with_category_and_weight.php
Go to the documentation of this file.
1 <?php
17 use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryExtensionFactory;
18 
20 
21 require __DIR__ . '/configurable_attribute.php';
22 
24 $productRepository = Bootstrap::getObjectManager()
25  ->create(ProductRepositoryInterface::class);
26 
28 $installer = Bootstrap::getObjectManager()->create(CategorySetup::class);
29 
30 /* Create simple products per each option value*/
32 $options = $attribute->getOptions();
33 
35 $attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
37 $productIds = [10, 20];
38 array_shift($options); //remove the first option which is empty
39 
40 foreach ($options as $option) {
42  $product = Bootstrap::getObjectManager()->create(Product::class);
43  $productId = array_shift($productIds);
44  $product->setTypeId(Type::TYPE_SIMPLE)
45  ->setId($productId)
46  ->setAttributeSetId($attributeSetId)
47  ->setWebsiteIds([1])
48  ->setName('Configurable Option' . $option->getLabel())
49  ->setSku('simple_' . $productId)
50  ->setPrice($productId)
51  ->setTestConfigurable($option->getValue())
52  ->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
53  ->setStatus(Status::STATUS_ENABLED)
54  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
56  'category_ids' => [2]
57  ];
58  foreach ($eavAttributeValues as $eavCategoryAttributeCode => $eavCategoryAttributeValues) {
59  $product->setCustomAttribute($eavCategoryAttributeCode, $eavCategoryAttributeValues);
60  }
61 
63 
68 
74  \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory::class
75  );
76 
80  $imageContentFactory = $objectManager->get(\Magento\Framework\Api\Data\ImageContentInterfaceFactory::class);
82  $testImagePath = __DIR__ .'/magento_image.jpg';
83  $imageContent->setBase64EncodedData(base64_encode(file_get_contents($testImagePath)));
84  $imageContent->setType("image/jpeg");
85  $imageContent->setName("1.jpg");
86 
88  $video->setDisabled(false);
89  $video->setFile('1.jpg');
90  $video->setLabel('Video Label');
91  $video->setMediaType('external-video');
92  $video->setPosition(2);
93  $video->setContent($imageContent);
94 
99  \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryExtensionFactory::class
100  );
102 
107  \Magento\Framework\Api\Data\VideoContentInterfaceFactory::class
108  );
110  $videoContent->setMediaType('external-video');
111  $videoContent->setVideoDescription('Video description');
112  $videoContent->setVideoProvider('youtube');
113  $videoContent->setVideoMetadata('Video Metadata');
114  $videoContent->setVideoTitle('Video title');
115  $videoContent->setVideoUrl('http://www.youtube.com/v/tH_2PFNmWoga');
116 
117  $mediaGalleryEntryExtension->setVideoContent($videoContent);
118  $video->setExtensionAttributes($mediaGalleryEntryExtension);
119 
124  \Magento\Catalog\Api\ProductAttributeMediaGalleryManagementInterface::class
125  );
126  $mediaGalleryManagement->create('simple_' . $productId, $video);
127 
129  $stockItem = Bootstrap::getObjectManager()->create(\Magento\CatalogInventory\Model\Stock\Item::class);
130  $stockItem->load($productId, 'product_id');
131 
132  if (!$stockItem->getProductId()) {
133  $stockItem->setProductId($productId);
134  }
135  $stockItem->setUseConfigManageStock(1);
136  $stockItem->setQty(1000);
137  $stockItem->setIsQtyDecimal(0);
138  $stockItem->setIsInStock(1);
139  $stockItem->save();
140 
141  $attributeValues[] = [
142  'label' => 'test',
143  'attribute_id' => $attribute->getId(),
144  'value_index' => $option->getValue(),
145  ];
146  $associatedProductIds[] = $product->getId();
147 }
148 
150 $product = Bootstrap::getObjectManager()->create(Product::class);
151 
153 $optionsFactory = Bootstrap::getObjectManager()->create(Factory::class);
154 
156  [
157  'attribute_id' => $attribute->getId(),
158  'code' => $attribute->getAttributeCode(),
159  'label' => $attribute->getStoreLabel(),
160  'position' => '0',
161  'values' => $attributeValues,
162  ],
163 ];
164 
166 
167 $extensionConfigurableAttributes = $product->getExtensionAttributes();
168 $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
169 $extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
170 
171 $product->setExtensionAttributes($extensionConfigurableAttributes);
172 
173 // Remove any previously created product with the same id.
175 $registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
176 $registry->unregister('isSecureArea');
177 $registry->register('isSecureArea', true);
178 try {
179  $productToDelete = $productRepository->getById(1);
180  $productRepository->delete($productToDelete);
181 
183  $itemResource = Bootstrap::getObjectManager()->get(\Magento\Quote\Model\ResourceModel\Quote\Item::class);
184  $itemResource->getConnection()->delete(
185  $itemResource->getMainTable(),
186  'product_id = ' . $productToDelete->getId()
187  );
188 } catch (\Exception $e) {
189  // Nothing to remove
190 }
191 $registry->unregister('isSecureArea');
192 $registry->register('isSecureArea', false);
193 
194 $product->setTypeId(Configurable::TYPE_CODE)
195  ->setId(1)
196  ->setAttributeSetId($attributeSetId)
197  ->setWebsiteIds([1])
198  ->setName('Configurable Product')
199  ->setSku('configurable')
200  ->setVisibility(Visibility::VISIBILITY_BOTH)
201  ->setWeight(1)
202  ->setStatus(Status::STATUS_ENABLED)
203  ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
204 
206 
209  ->create(\Magento\Catalog\Api\CategoryLinkManagementInterface::class);
210 
211 $categoryLinkManagement->assignProductToCategories(
212  $product->getSku(),
213  [2]
214 );
$objectManager
Definition: bootstrap.php:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60