/** * {@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; }
/** * Fill data column * * @param ProductInterface $linkedProduct * @param ProductLinkInterface $linkItem * @return array */ protected function fillData(ProductInterface $linkedProduct, ProductLinkInterface $linkItem) { /** @var \Magento\Framework\Currency $currency */ $currency = $this->localeCurrency->getCurrency($this->locator->getBaseCurrencyCode()); return ['id' => $linkedProduct->getId(), 'name' => $linkedProduct->getName(), 'sku' => $linkItem->getLinkedProductSku(), 'price' => $currency->toCurrency(sprintf("%f", $linkedProduct->getPrice())), 'qty' => $linkItem->getExtensionAttributes()->getQty(), 'position' => $linkItem->getPosition(), 'thumbnail' => $this->imageHelper->init($linkedProduct, 'product_listing_thumbnail')->getUrl(), 'type_id' => $linkedProduct->getTypeId(), 'status' => $this->status->getOptionText($linkedProduct->getStatus()), 'attribute_set' => $this->attributeSetRepository->get($linkedProduct->getAttributeSetId())->getAttributeSetName()]; }
/** * {@inheritdoc} */ public function delete(\Magento\Catalog\Api\Data\ProductLinkInterface $entity) { $linkedProduct = $this->productRepository->get($entity->getLinkedProductSku()); $product = $this->productRepository->get($entity->getSku()); $linkTypesToId = $this->linkTypeProvider->getLinkTypes(); $productData = $this->metadataPool->getHydrator(ProductInterface::class)->extract($product); $linkId = $this->linkResource->getProductLinkId($productData[$this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()], $linkedProduct->getId(), $linkTypesToId[$entity->getLinkType()]); if (!$linkId) { throw new NoSuchEntityException(__('Product with SKU %1 is not linked to product with SKU %2', $entity->getLinkedProductSku(), $entity->getSku())); } try { $this->linkResource->deleteProductLink($linkId); } catch (\Exception $exception) { throw new CouldNotSaveException(__('Invalid data provided for linked products')); } return true; }
/** * Prepare data column * * @param ProductInterface $linkedProduct * @param ProductLinkInterface $linkItem * @return array */ protected function fillData(ProductInterface $linkedProduct, ProductLinkInterface $linkItem) { return ['id' => $linkedProduct->getId(), 'thumbnail' => $this->imageHelper->init($linkedProduct, 'product_listing_thumbnail')->getUrl(), 'name' => $linkedProduct->getName(), 'status' => $this->status->getOptionText($linkedProduct->getStatus()), 'attribute_set' => $this->attributeSetRepository->get($linkedProduct->getAttributeSetId())->getAttributeSetName(), 'sku' => $linkItem->getLinkedProductSku(), 'price' => $linkedProduct->getPrice(), 'position' => $linkItem->getPosition()]; }