Example #1
0
 /**
  * {@inheritdoc}
  */
 public function setProductLinks($sku, $type, array $items)
 {
     $linkTypes = $this->linkTypeProvider->getLinkTypes();
     if (!isset($linkTypes[$type])) {
         throw new NoSuchEntityException(__('Provided link type "%1" does not exist', $type));
     }
     $product = $this->productRepository->get($sku);
     $assignedSkuList = [];
     /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $link */
     foreach ($items as $link) {
         $assignedSkuList[] = $link->getLinkedProductSku();
     }
     $linkedProductIds = $this->productResource->getProductsIdsBySkus($assignedSkuList);
     $links = [];
     /** @var \Magento\Catalog\Api\Data\ProductLinkInterface[] $items*/
     foreach ($items as $link) {
         $data = $link->__toArray();
         $linkedSku = $link->getLinkedProductSku();
         if (!isset($linkedProductIds[$linkedSku])) {
             throw new NoSuchEntityException(__('Product with SKU "%1" does not exist', $linkedSku));
         }
         $data['product_id'] = $linkedProductIds[$linkedSku];
         $links[$linkedProductIds[$linkedSku]] = $data;
     }
     $this->linkInitializer->initializeLinks($product, [$type => $links]);
     try {
         $product->save();
     } catch (\Exception $exception) {
         throw new CouldNotSaveException(__('Invalid data provided for linked products'));
     }
     return true;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function assign($productSku, array $assignedProducts, $type)
 {
     $product = $this->productLoader->load($productSku);
     $assignedSkuList = array_map(function ($item) {
         return $item->getSku();
     }, $assignedProducts);
     $linkedProductIds = $this->productResource->getProductsIdsBySkus($assignedSkuList);
     $links = [];
     /** @var Data\ProductLink[] $assignedProducts*/
     foreach ($assignedProducts as $linkedProduct) {
         $data = $linkedProduct->__toArray();
         if (!isset($linkedProductIds[$linkedProduct->getSku()])) {
             throw new NoSuchEntityException(sprintf("Product with SKU \"%s\" does not exist", $linkedProduct->getSku()));
         }
         $data['product_id'] = $linkedProductIds[$linkedProduct->getSku()];
         $links[$linkedProductIds[$linkedProduct->getSku()]] = $data;
     }
     $this->saveLinks($product, [$type => $links]);
     return true;
 }
Example #3
0
 /**
  * Process product links, creating new links, updating and deleting existing links
  *
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @param \Magento\Catalog\Api\Data\ProductLinkInterface[] $newLinks
  * @return $this
  * @throws NoSuchEntityException
  */
 private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $product, $newLinks)
 {
     if ($newLinks === null) {
         // If product links were not specified, don't do anything
         return $this;
     }
     // Clear all existing product links and then set the ones we want
     $linkTypes = $this->linkTypeProvider->getLinkTypes();
     foreach (array_keys($linkTypes) as $typeName) {
         $this->linkInitializer->initializeLinks($product, [$typeName => []]);
     }
     // Set each linktype info
     if (!empty($newLinks)) {
         $productLinks = [];
         foreach ($newLinks as $link) {
             $productLinks[$link->getLinkType()][] = $link;
         }
         foreach ($productLinks as $type => $linksByType) {
             $assignedSkuList = [];
             /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $link */
             foreach ($linksByType as $link) {
                 $assignedSkuList[] = $link->getLinkedProductSku();
             }
             $linkedProductIds = $this->resourceModel->getProductsIdsBySkus($assignedSkuList);
             $linksToInitialize = [];
             foreach ($linksByType as $link) {
                 $linkDataArray = $this->extensibleDataObjectConverter->toNestedArray($link, [], 'Magento\\Catalog\\Api\\Data\\ProductLinkInterface');
                 $linkedSku = $link->getLinkedProductSku();
                 if (!isset($linkedProductIds[$linkedSku])) {
                     throw new NoSuchEntityException(__('Product with SKU "%1" does not exist', $linkedSku));
                 }
                 $linkDataArray['product_id'] = $linkedProductIds[$linkedSku];
                 $linksToInitialize[$linkedProductIds[$linkedSku]] = $linkDataArray;
             }
             $this->linkInitializer->initializeLinks($product, [$type => $linksToInitialize]);
         }
     }
     $product->setProductLinks($newLinks);
     return $this;
 }