Skip to content

Commit 80ada63

Browse files
committed
Create addCustomOptions.php
1 parent 5b45e17 commit 80ada63

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

addCustomOptions.php

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
require dirname(__FILE__) . '/app/bootstrap.php';
4+
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
5+
6+
class Outslide extends \Magento\Framework\App\Http
7+
implements \Magento\Framework\AppInterface {
8+
public function launch()
9+
{
10+
$appState = $this->_objectManager->get('\Magento\Framework\App\State');
11+
$appState->setAreaCode('adminhtml');
12+
13+
$productRepository = $this->_objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
14+
$optionFactory = $this->_objectManager->get('\Magento\Catalog\Model\Product\OptionFactory');
15+
16+
$productIds = [
17+
10,
18+
25
19+
];
20+
21+
foreach ($productIds as $productId) {
22+
try{
23+
$_product = $productRepository->getById($productId);
24+
$optionsArray = [
25+
[
26+
'title' => 'Select option',
27+
'type' => 'drop_down',
28+
'is_require' => 1,
29+
'sort_order' => 1,
30+
'values' => [
31+
[
32+
'title' => 'Option 1',
33+
'price' => 10,
34+
'price_type' => 'fixed',
35+
'sku' => 'Option 1 sku',
36+
'sort_order' => 1,
37+
],
38+
[
39+
'title' => 'Option 2',
40+
'price' => 10,
41+
'price_type' => 'fixed',
42+
'sku' => 'Option 2 sku',
43+
'sort_order' => 2,
44+
],
45+
[
46+
'title' => 'Option 3',
47+
'price' => 10,
48+
'price_type' => 'fixed',
49+
'sku' => 'Option 3 sku',
50+
'sort_order' => 3,
51+
],
52+
],
53+
]
54+
];
55+
56+
foreach ($optionsArray as $optionValue) {
57+
$option = $optionFactory->create()->setProductId($_product->getId())
58+
->setStoreId($_product->getStoreId())
59+
->addData($optionValue);
60+
$option->save();
61+
$_product->addOption($option);
62+
// must save product to add options in product
63+
$productRepository->save($_product);
64+
}
65+
66+
} catch (\Exception $e) {
67+
echo $e->getMessage();
68+
69+
}
70+
71+
}
72+
73+
echo 'Done!';
74+
//the method must end with this line
75+
return $this->_response;
76+
}
77+
}
78+
79+
/** @var \Magento\Framework\App\Http $app */
80+
$app = $bootstrap->createApplication('Outslide');
81+
$bootstrap->run($app);

0 commit comments

Comments
 (0)