Create test.php file magento2 root directory and copy below code
<?php
echo '<pre>';
error_reporting(0);
ini_set('display_errors', 1);
use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$url = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
$websiteId = $storeManager->getWebsite()->getWebsiteId();
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/importmissingorders.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
//die("here");
$productId = 2;
$product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
$values = [
[
'record_id'=>0,
'title'=>'Red',
'price'=>10,
'price_type'=>"fixed",
'sort_order'=>1,
'is_delete'=>0
],
[
'record_id'=>1,
'title'=>'White',
'price'=>10,
'price_type'=>"fixed",
'sort_order'=>1,
'is_delete'=>0
],
[
'record_id'=>2,
'title'=>'Black',
'price'=>10,
'price_type'=>"fixed",
'sort_order'=>1,
'is_delete'=>0
]
];
$options = [
[
"sort_order" => 2,
"title" => "Color",
"price_type" => "fixed",
"price" => "",
"type" => "drop_down",
"is_require" => 0,
"values" => $values
]
];
$product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($options as $arrayOption) {
$option = $objectManager->create('\Magento\Catalog\Model\Product\Option')
->setProductId($productId)
->setStoreId($product->getStoreId())
->addData($arrayOption);
$option->save();
$product->addOption($option);
}
?>
after saving the file excute domain.com/test.php
<?php
echo '<pre>';
error_reporting(0);
ini_set('display_errors', 1);
use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$url = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
$websiteId = $storeManager->getWebsite()->getWebsiteId();
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/importmissingorders.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
//die("here");
$productId = 2;
$product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
$values = [
[
'record_id'=>0,
'title'=>'Red',
'price'=>10,
'price_type'=>"fixed",
'sort_order'=>1,
'is_delete'=>0
],
[
'record_id'=>1,
'title'=>'White',
'price'=>10,
'price_type'=>"fixed",
'sort_order'=>1,
'is_delete'=>0
],
[
'record_id'=>2,
'title'=>'Black',
'price'=>10,
'price_type'=>"fixed",
'sort_order'=>1,
'is_delete'=>0
]
];
$options = [
[
"sort_order" => 2,
"title" => "Color",
"price_type" => "fixed",
"price" => "",
"type" => "drop_down",
"is_require" => 0,
"values" => $values
]
];
$product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($options as $arrayOption) {
$option = $objectManager->create('\Magento\Catalog\Model\Product\Option')
->setProductId($productId)
->setStoreId($product->getStoreId())
->addData($arrayOption);
$option->save();
$product->addOption($option);
}
?>
after saving the file excute domain.com/test.php
Comments
Post a Comment