protected function setUp() { $this->objectManager = new ObjectManager($this); $this->locatorMock = $this->getMockBuilder(LocatorInterface::class)->getMockForAbstractClass(); $this->productMock = $this->getMockBuilder(ProductInterface::class)->setMethods(['getId', 'getTypeId'])->getMockForAbstractClass(); $this->productMock->expects($this->any())->method('getId')->willReturn(self::PRODUCT_ID); $this->productMock->expects($this->any())->method('getTypeId')->willReturn(GroupedProductType::TYPE_CODE); $this->linkedProductMock = $this->getMockBuilder(ProductInterface::class)->setMethods(['getId', 'getName', 'getPrice'])->getMockForAbstractClass(); $this->linkedProductMock->expects($this->any())->method('getId')->willReturn(self::LINKED_PRODUCT_ID); $this->linkedProductMock->expects($this->any())->method('getName')->willReturn(self::LINKED_PRODUCT_NAME); $this->linkedProductMock->expects($this->any())->method('getPrice')->willReturn(self::LINKED_PRODUCT_PRICE); $this->linkMock = $this->getMockBuilder(ProductLinkInterface::class)->setMethods(['getLinkType', 'getLinkedProductSku', 'getPosition', 'getExtensionAttributes'])->getMockForAbstractClass(); $this->linkExtensionMock = $this->getMockBuilder(ProductLinkExtensionInterface::class)->setMethods(['getQty'])->getMockForAbstractClass(); $this->linkExtensionMock->expects($this->any())->method('getQty')->willReturn(self::LINKED_PRODUCT_QTY); $this->linkMock->expects($this->any())->method('getExtensionAttributes')->willReturn($this->linkExtensionMock); $this->linkMock->expects($this->any())->method('getPosition')->willReturn(self::LINKED_PRODUCT_POSITION); $this->linkMock->expects($this->any())->method('getLinkedProductSku')->willReturn(self::LINKED_PRODUCT_SKU); $this->linkMock->expects($this->any())->method('getLinkType')->willReturn(Grouped::LINK_TYPE); $this->linkRepositoryMock = $this->getMockBuilder(ProductLinkRepositoryInterface::class)->setMethods(['getList'])->getMockForAbstractClass(); $this->linkRepositoryMock->expects($this->any())->method('getList')->with($this->productMock)->willReturn([$this->linkMock]); $this->productRepositoryMock = $this->getMockBuilder(ProductRepositoryInterface::class)->setMethods(['get'])->getMockForAbstractClass(); $this->productRepositoryMock->expects($this->any())->method('get')->with(self::LINKED_PRODUCT_SKU)->willReturn($this->linkedProductMock); $this->storeMock = $this->getMockBuilder(StoreInterface::class)->setMethods(['getId'])->getMockForAbstractClass(); $this->locatorMock->expects($this->any())->method('getProduct')->willReturn($this->productMock); $this->locatorMock->expects($this->any())->method('getStore')->willReturn($this->storeMock); }
/** * {@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()]; }