Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $custom_options ="4:fixed:0:0;4.5:fixed:0:1;5:fixed:0:2;";
- $Products = $productCollection->create()
- ->addAttributeToSelect(['id','sku'])
- ->addOrder('entity_id','desc')->load();
- foreach ($Products as $Product) {
- insertProductCustomOptions($Product->getId(), $custom_options, $custom_option_label, $Product->getSku(), 1);
- }
- function insertProductCustomOptions($product_id, $option_data, $option_title, $sku, $is_require) {
- //
- try{
- $objectManager = MagentoFrameworkAppObjectManager::getInstance();
- // check if product option is already there
- $product = $objectManager->create('MagentoCatalogModelProduct')->load($product_id);
- $customOptions = $objectManager->get('MagentoCatalogModelProductOption')->getProductOptionCollection($product);
- // Set option array
- $allOptionsArray = $customOptions->getData();
- $flag = 0;
- // Collect all options values
- foreach ($allOptionsArray as $allOpKey => $allOpVal) {
- if($allOpVal['title'] == $option_title) {
- $flag = 1;
- break;
- }
- }
- if ($flag == 1) {
- $optionValues = explode(';', $option_data);
- $options_arr = array();
- $cos = $co = array();
- foreach ($optionValues as $optionValue) {
- $optionValueData = explode(':', $optionValue);
- if (count($optionValueData) >= 3) {
- $optionValueDataLabel = $optionValueData[0];
- $optionValueDataPriceType = $optionValueData[1];
- $optionValueDataPrice = $optionValueData[2];
- $optionValueDataSortOrder = 1;
- if (count($optionValueData) >= 4) {
- $optionValueDataSortOrder = $optionValueData[3];
- }
- $option_sku = $sku . ' ' . $optionValueDataLabel;
- $options_arr[$option_sku] = array('title' => $optionValueDataLabel, 'price' => $optionValueDataPrice, 'price_type' => $optionValueDataPriceType, 'sku' => $sku . ' ' . $optionValueDataLabel, 'sort_order' => $optionValueDataSortOrder, 'exist_flag' => 0, );
- }
- }
- // Get all options
- foreach ($customOptions as $o) {
- if ($o -> getTitle() == $option_title) {
- $values = $o -> getValuesCollection();
- foreach ($values as $k => $v) {
- $option_sku_existing = $v -> getSku();
- if (array_key_exists($option_sku_existing, $options_arr)) {
- $v -> setTitle($options_arr[$option_sku_existing]['title']) -> setSku($option_sku_existing) -> setPriceType($options_arr[$option_sku_existing]['price_type']) -> setSortOrder($options_arr[$option_sku_existing]['sort_order']) -> setPrice(floatval($options_arr[$option_sku_existing]['price']));
- // set exist flasg to 1
- $options_arr[$option_sku_existing]['exist_flag'] = 1;
- $v -> setOption($o) -> save();
- $cos[] = $v -> toArray($co);
- } else {
- // remove all other skus which are not present
- $v -> delete();
- }
- }
- // Option array
- foreach ($options_arr as $osku => $oval) {
- if ($oval['exist_flag'] == 0) {
- // add those values which are not there already
- $value = $objectManager->create("MagentoCatalogModelProductOptionValue");
- $value -> setOption($o) -> setTitle($oval['title']) -> setSku($oval['sku']) -> setPriceType($oval['price_type']) -> setSortOrder($oval['sort_order']) -> setPrice(floatval($oval['price'])) -> setOptionId($o -> getId());
- $value -> save();
- $cos[] = $value -> toArray($co);
- }
- }
- $o -> setData("values", $cos) -> save();
- }
- }
- } else {
- // add all new attributes
- $options_arr[$sku] = array('title' => $option_title, 'type' => 'drop_down', 'is_require' => $is_require, 'sort_order' => 0, 'values' => array());
- $optionValues = explode(';', $option_data);
- foreach ($optionValues as $optionValue) {
- $optionValueData = explode(':', $optionValue);
- if (count($optionValueData) >= 3) {
- $optionValueDataLabel = $optionValueData[0];
- $optionValueDataPriceType = $optionValueData[1];
- $optionValueDataPrice = $optionValueData[2];
- $optionValueDataSortOrder = 0;
- if (count($optionValueData) >= 4) {
- $optionValueDataSortOrder = $optionValueData[3];
- }
- $options_arr[$sku]['values'][] = array('title' => $optionValueDataLabel, 'price' => $optionValueDataPrice, 'price_type' => $optionValueDataPriceType, 'sku' => $sku . ' ' . $optionValueDataLabel, 'sort_order' => $optionValueDataSortOrder);
- }
- }
- $product -> setCanSaveCustomOptions(true);
- $product -> setHasOptions(1);
- foreach ($options_arr as $sku => $customOptions) {
- $option = $objectManager->create('MagentoCatalogModelProductOption')
- ->setProductId($product_id)
- ->setStoreId($product->getStoreId())
- ->addData($customOptions);
- $option->save();
- $product->addOption($option);
- }
- unset($options_arr);
- unset($optionValues);
- }
- }
- catch (Exception $e)
- {
- echo $e->getMessage();
- }
Add Comment
Please, Sign In to add comment