Ejemplo n.º 1
0
 /**
  * Get product links list
  *
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @return \Magento\Catalog\Api\Data\ProductLinkInterface[]
  */
 public function getList(\Magento\Catalog\Api\Data\ProductInterface $product)
 {
     $output = [];
     $linkTypes = $this->linkTypeProvider->getLinkTypes();
     foreach (array_keys($linkTypes) as $linkTypeName) {
         $collection = $this->entityCollectionProvider->getCollection($product, $linkTypeName);
         foreach ($collection as $item) {
             /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
             $productLink = $this->productLinkFactory->create();
             $productLink->setSku($product->getSku())->setLinkType($linkTypeName)->setLinkedProductSku($item['sku'])->setLinkedProductType($item['type'])->setPosition($item['position']);
             if (isset($item['custom_attributes'])) {
                 $productLinkExtension = $productLink->getExtensionAttributes();
                 if ($productLinkExtension === null) {
                     $productLinkExtension = $this->productLinkExtensionFactory->create();
                 }
                 foreach ($item['custom_attributes'] as $option) {
                     $name = $option['attribute_code'];
                     $value = $option['value'];
                     $setterName = 'set' . ucfirst($name);
                     // Check if setter exists
                     if (method_exists($productLinkExtension, $setterName)) {
                         call_user_func([$productLinkExtension, $setterName], $value);
                     }
                 }
                 $productLink->setExtensionAttributes($productLinkExtension);
             }
             $output[] = $productLink;
         }
     }
     return $output;
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function delete(\Magento\Catalog\Api\Data\ProductLinkInterface $entity)
 {
     $linkedProduct = $this->productRepository->get($entity->getLinkedProductSku());
     $product = $this->productRepository->get($entity->getProductSku());
     $links = $this->entityCollectionProvider->getCollection($product, $entity->getLinkType());
     if (!isset($links[$linkedProduct->getId()])) {
         throw new NoSuchEntityException(__('Product with SKU %1 is not linked to product with SKU %2', $entity->getLinkedProductSku(), $entity->getProductSku()));
     }
     //Remove product from the linked product list
     unset($links[$linkedProduct->getId()]);
     $this->linkInitializer->initializeLinks($product, [$entity->getLinkType() => $links]);
     try {
         $product->save();
     } catch (\Exception $exception) {
         throw new CouldNotSaveException(__('Invalid data provided for linked products'));
     }
     return true;
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function getLinkedItemsByType($sku, $type)
 {
     $output = [];
     $product = $this->productRepository->get($sku);
     try {
         $collection = $this->entityCollectionProvider->getCollection($product, $type);
     } catch (NoSuchEntityException $e) {
         throw new NoSuchEntityException(__('Unknown link type: %1', (string) $type));
     }
     foreach ($collection as $item) {
         /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
         $productLink = $this->productLinkFactory->create();
         $productLink->setProductSku($product->getSku())->setLinkType($type)->setLinkedProductSku($item['sku'])->setLinkedProductType($item['type'])->setPosition($item['position']);
         if (isset($item['custom_attributes'])) {
             foreach ($item['custom_attributes'] as $option) {
                 $productLink->getExtensionAttributes()->setQty($option['value']);
             }
         }
         $output[] = $productLink;
     }
     return $output;
 }