Exemplo n.º 1
0
 /**
  * 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;
 }