/** * @param string $entityType * @param object $entity * @return \Magento\Catalog\Api\Data\ProductInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entityType, $entity) { /** @var \Magento\Catalog\Api\Data\ProductInterface $entity*/ foreach ($this->productLinkRepository->getList($entity) as $link) { $this->productLinkRepository->delete($link); } foreach ($entity->getProductLinks() as $link) { $this->productLinkRepository->save($link); } return $entity; }
/** * Get product links info * * @return \Magento\Catalog\Api\Data\ProductLinkInterface[] */ public function getProductLinks() { if ($this->_links === null) { $this->_links = $this->linkRepository->getList($this); } return $this->_links; }
/** * {@inheritdoc} */ public function modifyData(array $data) { /** @var \Magento\Catalog\Model\Product $product */ $product = $this->locator->getProduct(); $productId = $product->getId(); if (!$productId) { return $data; } $priceModifier = $this->getPriceModifier(); /** * Set field name for modifier */ $priceModifier->setData('name', 'price'); foreach ($this->getDataScopes() as $dataScope) { $data[$productId]['links'][$dataScope] = []; foreach ($this->productLinkRepository->getList($product) as $linkItem) { if ($linkItem->getLinkType() !== $dataScope) { continue; } /** @var \Magento\Catalog\Model\Product $linkedProduct */ $linkedProduct = $this->productRepository->get($linkItem->getLinkedProductSku(), false, $this->locator->getStore()->getId()); $data[$productId]['links'][$dataScope][] = $this->fillData($linkedProduct, $linkItem); } if (!empty($data[$productId]['links'][$dataScope])) { $dataMap = $priceModifier->prepareDataSource(['data' => ['items' => $data[$productId]['links'][$dataScope]]]); $data[$productId]['links'][$dataScope] = $dataMap['data']['items']; } } $data[$productId][self::DATA_SOURCE_DEFAULT]['current_product_id'] = $productId; $data[$productId][self::DATA_SOURCE_DEFAULT]['current_store_id'] = $this->locator->getStore()->getId(); return $data; }
/** * Add specific filters * * @param Collection $collection * @return Collection */ protected function addCollectionFilters(Collection $collection) { $relatedProducts = []; /** @var ProductLinkInterface $linkItem */ foreach ($this->productLinkRepository->getList($this->getProduct()) as $linkItem) { if ($linkItem->getLinkType() !== $this->getLinkType()) { continue; } $relatedProducts[] = $this->productRepository->get($linkItem->getLinkedProductSku())->getId(); } if ($relatedProducts) { $collection->addAttributeToFilter($collection->getIdFieldName(), ['nin' => [$relatedProducts]]); } return $collection; }
/** * {@inheritdoc} */ public function modifyData(array $data) { /** @var \Magento\Catalog\Model\Product $product */ $product = $this->locator->getProduct(); $modelId = $product->getId(); if ($modelId) { $storeId = $this->locator->getStore()->getId(); $data[$product->getId()]['links'][self::LINK_TYPE] = []; foreach ($this->productLinkRepository->getList($product) as $linkItem) { if ($linkItem->getLinkType() !== self::LINK_TYPE) { continue; } /** @var \Magento\Catalog\Api\Data\ProductInterface $linkedProduct */ $linkedProduct = $this->productRepository->get($linkItem->getLinkedProductSku(), false, $storeId); $data[$modelId]['links'][self::LINK_TYPE][] = $this->fillData($linkedProduct, $linkItem); } $data[$modelId][self::DATA_SOURCE_DEFAULT]['current_store_id'] = $storeId; } return $data; }
/** * {@inheritdoc} */ public function modifyData(array $data) { /** @var \Magento\Catalog\Model\Product $product */ $product = $this->locator->getProduct(); $modelId = $product->getId(); if ($modelId) { $storeId = $this->locator->getStore()->getId(); /** @var \Magento\Framework\Currency $currency */ $currency = $this->localeCurrency->getCurrency($this->locator->getBaseCurrencyCode()); $data[$product->getId()]['links'][self::LINK_TYPE] = []; foreach ($this->productLinkRepository->getList($product) as $linkItem) { if ($linkItem->getLinkType() !== self::LINK_TYPE) { continue; } /** @var \Magento\Catalog\Api\Data\ProductInterface $linkedProduct */ $linkedProduct = $this->productRepository->get($linkItem->getLinkedProductSku(), false, $storeId); $data[$modelId]['links'][self::LINK_TYPE][] = ['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()]; } $data[$modelId][self::DATA_SOURCE_DEFAULT]['current_store_id'] = $storeId; } return $data; }
/** * {@inheritdoc} */ public function modifyData(array $data) { /** @var \Magento\Catalog\Model\Product $product */ $product = $this->locator->getProduct(); $productId = $product->getId(); if (!$productId) { return $data; } foreach ($this->getDataScopes() as $dataScope) { $data[$productId]['links'][$dataScope] = []; foreach ($this->productLinkRepository->getList($product) as $linkItem) { if ($linkItem->getLinkType() !== $dataScope) { continue; } /** @var \Magento\Catalog\Model\Product $linkedProduct */ $linkedProduct = $this->productRepository->get($linkItem->getLinkedProductSku(), false, $this->locator->getStore()->getId()); $data[$productId]['links'][$dataScope][] = $this->fillData($linkedProduct, $linkItem); } } $data[$productId][self::DATA_SOURCE_DEFAULT]['current_product_id'] = $productId; $data[$productId][self::DATA_SOURCE_DEFAULT]['current_store_id'] = $this->locator->getStore()->getId(); return $data; }
/** * {@inheritdoc} */ public function modifyData(array $data) { /** @var \Magento\Catalog\Model\Product $product */ $product = $this->locator->getProduct(); $productId = $product->getId(); if (!$productId) { return $data; } foreach ($this->getDataScopes() as $dataScope) { $data[$productId]['links'][$dataScope] = []; foreach ($this->productLinkRepository->getList($product) as $linkItem) { if ($linkItem->getLinkType() !== $dataScope) { continue; } /** @var \Magento\Catalog\Model\Product $linkedProduct */ $linkedProduct = $this->productRepository->get($linkItem->getLinkedProductSku(), false, $this->locator->getStore()->getId()); $data[$productId]['links'][$dataScope][] = ['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()]; } } $data[$productId][self::DATA_SOURCE_DEFAULT]['current_product_id'] = $productId; $data[$productId][self::DATA_SOURCE_DEFAULT]['current_store_id'] = $this->locator->getStore()->getId(); return $data; }