Example #1
0
 /**
  * {@inheritdoc}
  */
 public function add($productSku, \Magento\Catalog\Service\V1\Product\CustomOptions\Data\Option $option)
 {
     $product = $this->productRepository->get($productSku);
     $optionData = $this->optionConverter->convert($option);
     $product->setCanSaveCustomOptions(true);
     $product->setProductOptions([$optionData]);
     $existingOptions = $product->getOptions();
     try {
         $product->save();
     } catch (\Exception $e) {
         throw new CouldNotSaveException('Could not save product option');
     }
     $productId = $product->getId();
     $product->reset();
     $product->load($productId);
     $currentOptions = $product->getOptions();
     $newID = array_diff(array_keys($currentOptions), array_keys($existingOptions));
     if (empty($newID)) {
         throw new CouldNotSaveException('Could not save product option');
     }
     $newID = current($newID);
     /** @var \Magento\Catalog\Model\Product\Option $newOption */
     $newOption = $currentOptions[$newID];
     $data = array(Data\Option::OPTION_ID => $newOption->getId(), Data\Option::TITLE => $newOption->getTitle(), Data\Option::TYPE => $newOption->getType(), Data\Option::IS_REQUIRE => $newOption->getIsRequire(), Data\Option::SORT_ORDER => $newOption->getSortOrder(), Data\Option::METADATA => $this->optionMetadataReader->read($newOption));
     $optionDataObject = $this->optionBuilder->populateWithArray($data)->create();
     return $optionDataObject;
 }
Example #2
0
 /**
  * Create option data object
  *
  * @param \Magento\Catalog\Model\Product\Option $option
  * @return \Magento\Catalog\Service\V1\Product\CustomOptions\Data\Option array
  */
 protected function _createOptionDataObject(\Magento\Catalog\Model\Product\Option $option)
 {
     $data = array(Data\Option::OPTION_ID => $option->getId(), Data\Option::TITLE => $option->getTitle(), Data\Option::TYPE => $option->getType(), Data\Option::IS_REQUIRE => $option->getIsRequire(), Data\Option::SORT_ORDER => $option->getSortOrder(), Data\Option::METADATA => $this->optionMetadataReader->read($option));
     return $this->optionBuilder->populateWithArray($data)->create();
 }