/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param \Magento\Downloadable\Api\Data\LinkInterface[] $links * @return $this */ protected function saveLinks(\Magento\Catalog\Api\Data\ProductInterface $product, array $links) { $existingLinkIds = []; //get existing links from extension attribute $extensionAttributes = $product->getExtensionAttributes(); if ($extensionAttributes !== null) { $existingLinks = $extensionAttributes->getDownloadableProductLinks(); if ($existingLinks !== null) { foreach ($existingLinks as $existingLink) { $existingLinkIds[] = $existingLink->getId(); } } } $updatedLinkIds = []; foreach ($links as $link) { $linkId = $link->getId(); if ($linkId) { $updatedLinkIds[] = $linkId; } $this->linkRepository->save($product->getSku(), $link); } $linkIdsToDelete = array_diff($existingLinkIds, $updatedLinkIds); foreach ($linkIdsToDelete as $linkId) { $this->linkRepository->delete($linkId); } return $this; }
/** * @param string $entityType * @param object $entity * @return object * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entityType, $entity) { /** @var \Magento\Catalog\Api\Data\ProductInterface $entity */ foreach ($this->linkRepository->getList($entity->getSku()) as $link) { $this->linkRepository->delete($link->getId()); } return $entity; }
/** * @param object $entity * @param array $arguments * @return \Magento\Catalog\Api\Data\ProductInterface|object * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entity, $arguments = []) { if ($entity->getTypeId() != \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { return $entity; } /** @var \Magento\Catalog\Api\Data\ProductInterface $entity */ foreach ($this->linkRepository->getList($entity->getSku()) as $link) { $this->linkRepository->delete($link->getId()); } return $entity; }
public function testExecuteNonDownloadable() { /** @var ProductInterface|\PHPUnit_Framework_MockObject_MockObject $entityMock */ $entityMock = $this->getMockBuilder(ProductInterface::class)->setMethods(['getTypeId', 'getExtensionAttributes', 'getSku', 'getStoreId'])->getMockForAbstractClass(); $entityMock->expects($this->once())->method('getTypeId')->willReturn(Type::TYPE_DOWNLOADABLE . 'some'); $entityMock->expects($this->never())->method('getExtensionAttributes'); $entityMock->expects($this->never())->method('getSku'); $entityMock->expects($this->never())->method('getStoreId'); $this->linkRepositoryMock->expects($this->never())->method('getList'); $this->linkRepositoryMock->expects($this->never())->method('save'); $this->linkRepositoryMock->expects($this->never())->method('delete'); $this->assertEquals($entityMock, $this->model->execute($entityMock)); }
/** * @param object $entity * @param array $arguments * @return \Magento\Catalog\Api\Data\ProductInterface|object * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entity, $arguments = []) { /** @var $entity \Magento\Catalog\Api\Data\ProductInterface */ if ($entity->getTypeId() != \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { return $entity; } $entityExtension = $entity->getExtensionAttributes(); $links = $this->linkRepository->getLinksByProduct($entity); if ($links) { $entityExtension->setDownloadableProductLinks($links); } $entity->setExtensionAttributes($entityExtension); return $entity; }
/** * @param object $entity * @param array $arguments * @return \Magento\Catalog\Api\Data\ProductInterface|object * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entity, $arguments = []) { /** @var $entity \Magento\Catalog\Api\Data\ProductInterface */ if ($entity->getTypeId() != Type::TYPE_DOWNLOADABLE) { return $entity; } /** @var \Magento\Downloadable\Api\Data\LinkInterface[] $links */ $links = $entity->getExtensionAttributes()->getDownloadableProductLinks() ?: []; foreach ($links as $link) { $link->setId(null); $this->linkRepository->save($entity->getSku(), $link, !(bool) $entity->getStoreId()); } return $entity; }
/** * @param string $entityType * @param object $entity * @return object * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entityType, $entity) { if ($entity->getTypeId() !== 'downloadable') { return $entity; } /** @var \Magento\Catalog\Api\Data\ProductInterface $entity */ foreach ($this->linkRepository->getList($entity->getSku()) as $link) { $this->linkRepository->delete($link->getId()); } $links = $entity->getExtensionAttributes()->getDownloadableProductLinks() ?: []; foreach ($links as $link) { $this->linkRepository->save($entity->getSku(), $link, !(bool) $entity->getStoreId()); } return $entity; }
/** * @param \Magento\Catalog\Model\Product $product * @return \Magento\Catalog\Model\Product */ public function afterLoad(\Magento\Catalog\Model\Product $product) { if ($product->getTypeId() != \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { return $product; } $productExtension = $product->getExtensionAttributes() ?: $this->productExtensionFactory->create(); $links = $this->linkRepository->getLinksByProduct($product); if ($links !== null) { $productExtension->setDownloadableProductLinks($links); } $samples = $this->sampleRepository->getSamplesByProduct($product); if ($samples !== null) { $productExtension->setDownloadableProductSamples($samples); } $product->setExtensionAttributes($productExtension); return $product; }
/** * @param string $entityType * @param object $entity * @param array $arguments * @return \Magento\Catalog\Api\Data\ProductInterface|object * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entityType, $entity, $arguments = []) { if ($entity->getTypeId() !== 'downloadable') { return $entity; } $links = $entity->getExtensionAttributes()->getDownloadableProductLinks() ?: []; $updatedLinks = []; $oldLinks = $this->linkRepository->getList($entity->getSku()); foreach ($links as $link) { if ($link->getId()) { $updatedLinks[$link->getId()] = true; } $this->linkRepository->save($entity->getSku(), $link, !(bool) $entity->getStoreId()); } /** @var \Magento\Catalog\Api\Data\ProductInterface $entity */ foreach ($oldLinks as $link) { if (!isset($updatedLinks[$link->getId()])) { $this->linkRepository->delete($link->getId()); } } return $entity; }