Guest User

Untitled

a guest
Jan 24th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. protected function getProductOptions($productObj)
  2. {
  3. $objectManager = MagentoFrameworkAppObjectManager::getInstance();
  4. $customOptions = $objectManager->get('MagentoCatalogModelProductOption')->getProductOptionCollection($productObj);
  5.  
  6. $options = [];
  7. if($customOptions->count() > 0) {
  8. foreach($customOptions as $customOption) {
  9.  
  10. $optionsValue = [];
  11. $customOptionValues = $objectManager->get('MagentoCatalogModelProductOptionValue')->getValuesCollection($customOption);
  12. if($customOptionValues->count() > 0) {
  13. foreach($customOptionValues as $customOptionValue) {
  14. $optionsValue[] = array(
  15. 'record_id' => $customOptionValue->getRecordId(),
  16. 'title' => $customOptionValue->getTitle(),
  17. 'price' => $customOptionValue->getPrice(),
  18. 'price_type' => $customOptionValue->getPriceType(),
  19. 'sort_order' => $customOptionValue->getSortOrder(),
  20. 'sku' => $customOptionValue->getSku(),
  21. 'is_delete' => 0,
  22. );
  23. }
  24. }
  25.  
  26.  
  27. $sku = $customOption->getSku();
  28. $title = $customOption->getTitle();
  29. $type = $customOption->getType();
  30. $price = $customOption->getPrice();
  31. $price_type = $customOption->getPriceType();
  32. $record_id = $customOption->getRecordId();
  33.  
  34. $options[] = array(
  35. 'sort_order' => $customOption->getSortOrder(),
  36. 'title' => $title,
  37. 'price_type' => $price_type,
  38. 'price' => $price,
  39. 'type' => $type,
  40. 'values' => $optionsValue,
  41. 'is_require' => 1,
  42. );
  43.  
  44. }
  45. }
  46.  
  47. return $options;
  48. }
  49.  
  50. protected function addProductCustomOptions($product, $productOption)
  51. {
  52. $objectManager = MagentoFrameworkAppObjectManager::getInstance();
  53.  
  54. $product->setHasOptions(1);
  55. $product->setCanSaveCustomOptions(true);
  56. foreach ($productOption as $arrayOption) {
  57. $option = $objectManager->create('MagentoCatalogModelProductOption')
  58. ->setProductId($product->getId())
  59. ->setStoreId($product->getStoreId())
  60. ->addData($arrayOption);
  61. $option->save();
  62. $product->addOption($option);
  63. }
  64.  
  65. }
Add Comment
Please, Sign In to add comment