Exemple #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();
 }
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 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());
     if (!$option->getOptionId()) {
         $option->setDefaultTitle($option->getTitle());
         $linksToAdd = is_array($option->getProductLinks()) ? $option->getProductLinks() : [];
     } else {
         $optionCollection = $this->type->getOptionsCollection($product);
         $optionCollection->setIdFilter($option->getOptionId());
         /** @var \Magento\Bundle\Model\Option $existingOption */
         $existingOption = $optionCollection->getFirstItem();
         if (!$existingOption->getOptionId()) {
             throw new NoSuchEntityException(__('Requested option doesn\'t exist'));
         }
         $option->setData(array_merge($existingOption->getData(), $option->getData()));
         /** @var \Magento\Bundle\Api\Data\LinkInterface[] $existingLinks */
         $existingLinks = is_array($existingOption->getProductLinks()) ? $existingOption->getProductLinks() : [];
         /** @var \Magento\Bundle\Api\Data\LinkInterface[] $newProductLinks */
         $newProductLinks = is_array($option->getProductLinks()) ? $option->getProductLinks() : [];
         /** @var \Magento\Bundle\Api\Data\LinkInterface[] $linksToDelete */
         $linksToDelete = array_udiff($existingLinks, $newProductLinks, [$this, 'compareLinks']);
         foreach ($linksToDelete as $link) {
             $this->linkManagement->removeChild($product->getSku(), $option->getOptionId(), $link->getSku());
         }
         /** @var \Magento\Bundle\Api\Data\LinkInterface[] $linksToAdd */
         $linksToAdd = array_udiff($newProductLinks, $existingLinks, [$this, 'compareLinks']);
     }
     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();
 }