Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function save(\Magento\Catalog\Api\Data\ProductInterface $product, \Magento\Bundle\Api\Data\OptionInterface $option)
 {
     $option->setStoreId($this->storeManager->getStore()->getId());
     $option->setParentId($product->getId());
     $optionId = $option->getOptionId();
     $linksToAdd = [];
     if (!$optionId) {
         $option->setDefaultTitle($option->getTitle());
         if (is_array($option->getProductLinks())) {
             $linksToAdd = $option->getProductLinks();
         }
     } else {
         $optionCollection = $this->type->getOptionsCollection($product);
         /** @var \Magento\Bundle\Model\Option $existingOption */
         $existingOption = $optionCollection->getItemById($option->getOptionId());
         if (!isset($existingOption) || !$existingOption->getOptionId()) {
             throw new NoSuchEntityException(__('Requested option doesn\'t exist'));
         }
         $option->setData(array_merge($existingOption->getData(), $option->getData()));
         $this->updateOptionSelection($product, $option);
     }
     try {
         $this->optionResource->save($option);
     } catch (\Exception $e) {
         throw new CouldNotSaveException(__('Could not save option'), $e);
     }
     /** @var \Magento\Bundle\Api\Data\LinkInterface $linkedProduct */
     foreach ($linksToAdd as $linkedProduct) {
         $this->linkManagement->addChild($product, $option->getOptionId(), $linkedProduct);
     }
     return $option->getOptionId();
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function save(\Magento\Catalog\Api\Data\ProductInterface $product, \Magento\Bundle\Api\Data\OptionInterface $option)
 {
     $metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
     $option->setStoreId($product->getStoreId());
     $option->setParentId($product->getData($metadata->getLinkField()));
     $linksToAdd = [];
     $option->setDefaultTitle($option->getDefaultTitle() ?: $option->getTitle());
     if (is_array($option->getProductLinks())) {
         $linksToAdd = $option->getProductLinks();
     }
     try {
         $this->optionResource->save($option);
     } catch (\Exception $e) {
         throw new CouldNotSaveException(__('Could not save option'), $e);
     }
     /** @var \Magento\Bundle\Api\Data\LinkInterface $linkedProduct */
     foreach ($linksToAdd as $linkedProduct) {
         $this->linkManagement->addChild($product, $option->getOptionId(), $linkedProduct);
     }
     $product->setIsRelationsChanged(true);
     return $option->getOptionId();
 }