Guest User

Untitled

a guest
May 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. $custom_options ="4:fixed:0:0;4.5:fixed:0:1;5:fixed:0:2;";
  2.  
  3.  
  4.  
  5. $Products = $productCollection->create()
  6. ->addAttributeToSelect(['id','sku'])
  7. ->addOrder('entity_id','desc')->load();
  8.  
  9.  
  10. foreach ($Products as $Product) {
  11. insertProductCustomOptions($Product->getId(), $custom_options, $custom_option_label, $Product->getSku(), 1);
  12. }
  13.  
  14.  
  15. function insertProductCustomOptions($product_id, $option_data, $option_title, $sku, $is_require) {
  16. //
  17.  
  18. try{
  19.  
  20. $objectManager = MagentoFrameworkAppObjectManager::getInstance();
  21. // check if product option is already there
  22. $product = $objectManager->create('MagentoCatalogModelProduct')->load($product_id);
  23. $customOptions = $objectManager->get('MagentoCatalogModelProductOption')->getProductOptionCollection($product);
  24. // Set option array
  25. $allOptionsArray = $customOptions->getData();
  26.  
  27. $flag = 0;
  28.  
  29. // Collect all options values
  30. foreach ($allOptionsArray as $allOpKey => $allOpVal) {
  31.  
  32.  
  33.  
  34. if($allOpVal['title'] == $option_title) {
  35.  
  36. $flag = 1;
  37. break;
  38. }
  39. }
  40. if ($flag == 1) {
  41.  
  42. $optionValues = explode(';', $option_data);
  43. $options_arr = array();
  44. $cos = $co = array();
  45. foreach ($optionValues as $optionValue) {
  46. $optionValueData = explode(':', $optionValue);
  47. if (count($optionValueData) >= 3) {
  48.  
  49. $optionValueDataLabel = $optionValueData[0];
  50. $optionValueDataPriceType = $optionValueData[1];
  51. $optionValueDataPrice = $optionValueData[2];
  52. $optionValueDataSortOrder = 1;
  53. if (count($optionValueData) >= 4) {
  54.  
  55.  
  56. $optionValueDataSortOrder = $optionValueData[3];
  57. }
  58. $option_sku = $sku . ' ' . $optionValueDataLabel;
  59. $options_arr[$option_sku] = array('title' => $optionValueDataLabel, 'price' => $optionValueDataPrice, 'price_type' => $optionValueDataPriceType, 'sku' => $sku . ' ' . $optionValueDataLabel, 'sort_order' => $optionValueDataSortOrder, 'exist_flag' => 0, );
  60. }
  61. }
  62. // Get all options
  63. foreach ($customOptions as $o) {
  64.  
  65. if ($o -> getTitle() == $option_title) {
  66. $values = $o -> getValuesCollection();
  67. foreach ($values as $k => $v) {
  68.  
  69. $option_sku_existing = $v -> getSku();
  70. if (array_key_exists($option_sku_existing, $options_arr)) {
  71.  
  72. $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']));
  73. // set exist flasg to 1
  74. $options_arr[$option_sku_existing]['exist_flag'] = 1;
  75. $v -> setOption($o) -> save();
  76. $cos[] = $v -> toArray($co);
  77. } else {
  78. // remove all other skus which are not present
  79. $v -> delete();
  80. }
  81. }
  82. // Option array
  83. foreach ($options_arr as $osku => $oval) {
  84. if ($oval['exist_flag'] == 0) {
  85.  
  86. // add those values which are not there already
  87. $value = $objectManager->create("MagentoCatalogModelProductOptionValue");
  88. $value -> setOption($o) -> setTitle($oval['title']) -> setSku($oval['sku']) -> setPriceType($oval['price_type']) -> setSortOrder($oval['sort_order']) -> setPrice(floatval($oval['price'])) -> setOptionId($o -> getId());
  89.  
  90. $value -> save();
  91. $cos[] = $value -> toArray($co);
  92.  
  93. }
  94.  
  95. }
  96. $o -> setData("values", $cos) -> save();
  97. }
  98. }
  99.  
  100. } else {
  101. // add all new attributes
  102. $options_arr[$sku] = array('title' => $option_title, 'type' => 'drop_down', 'is_require' => $is_require, 'sort_order' => 0, 'values' => array());
  103. $optionValues = explode(';', $option_data);
  104. foreach ($optionValues as $optionValue) {
  105. $optionValueData = explode(':', $optionValue);
  106. if (count($optionValueData) >= 3) {
  107. $optionValueDataLabel = $optionValueData[0];
  108. $optionValueDataPriceType = $optionValueData[1];
  109. $optionValueDataPrice = $optionValueData[2];
  110. $optionValueDataSortOrder = 0;
  111. if (count($optionValueData) >= 4) {
  112. $optionValueDataSortOrder = $optionValueData[3];
  113. }
  114. $options_arr[$sku]['values'][] = array('title' => $optionValueDataLabel, 'price' => $optionValueDataPrice, 'price_type' => $optionValueDataPriceType, 'sku' => $sku . ' ' . $optionValueDataLabel, 'sort_order' => $optionValueDataSortOrder);
  115. }
  116. }
  117. $product -> setCanSaveCustomOptions(true);
  118. $product -> setHasOptions(1);
  119. foreach ($options_arr as $sku => $customOptions) {
  120.  
  121. $option = $objectManager->create('MagentoCatalogModelProductOption')
  122. ->setProductId($product_id)
  123. ->setStoreId($product->getStoreId())
  124. ->addData($customOptions);
  125. $option->save();
  126. $product->addOption($option);
  127. }
  128. unset($options_arr);
  129. unset($optionValues);
  130.  
  131. }
  132.  
  133. }
  134. catch (Exception $e)
  135. {
  136. echo $e->getMessage();
  137. }
Add Comment
Please, Sign In to add comment