Beispiel #1
0
 /**
  * @param string $entitySku
  * @param \Magento\Bundle\Api\Data\OptionInterface $option
  * @return void
  */
 protected function removeOptionLinks($entitySku, $option)
 {
     $links = $option->getProductLinks();
     if (!empty($links)) {
         foreach ($links as $link) {
             $this->productLinkManagement->removeChild($entitySku, $option->getId(), $link->getSku());
         }
     }
 }
 /**
  * Update option selections
  *
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @param \Magento\Bundle\Api\Data\OptionInterface $option
  * @return $this
  */
 protected function updateOptionSelection(\Magento\Catalog\Api\Data\ProductInterface $product, \Magento\Bundle\Api\Data\OptionInterface $option)
 {
     $optionId = $option->getOptionId();
     $existingLinks = $this->linkManagement->getChildren($product->getSku(), $optionId);
     $linksToAdd = [];
     $linksToUpdate = [];
     $linksToDelete = [];
     if (is_array($option->getProductLinks())) {
         $productLinks = $option->getProductLinks();
         foreach ($productLinks as $productLink) {
             if (!$productLink->getId()) {
                 $linksToAdd[] = $productLink;
             } else {
                 $linksToUpdate[] = $productLink;
             }
         }
         /** @var \Magento\Bundle\Api\Data\LinkInterface[] $linksToDelete */
         $linksToDelete = $this->compareLinks($linksToUpdate, $existingLinks);
     }
     foreach ($linksToUpdate as $linkedProduct) {
         $this->linkManagement->saveChild($product->getSku(), $linkedProduct);
     }
     foreach ($linksToDelete as $linkedProduct) {
         $this->linkManagement->removeChild($product->getSku(), $option->getOptionId(), $linkedProduct->getSku());
     }
     foreach ($linksToAdd as $linkedProduct) {
         $this->linkManagement->addChild($product, $option->getOptionId(), $linkedProduct);
     }
     return $this;
 }
 /**
  * {@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();
 }