Example #1
0
 /**
  * @param string $entityType
  * @param object $entity
  * @return object
  * @throws CouldNotSaveException
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function execute($entityType, $entity)
 {
     /**
      * @var $entity \Magento\Catalog\Api\Data\ProductLinkInterface
      */
     $linkedProduct = $this->productRepository->get($entity->getLinkedProductSku());
     $product = $this->productRepository->get($entity->getSku());
     $links = [];
     $extensions = $this->dataObjectProcessor->buildOutputDataArray($entity->getExtensionAttributes(), 'Magento\\Catalog\\Api\\Data\\ProductLinkExtensionInterface');
     $extensions = is_array($extensions) ? $extensions : [];
     $data = $entity->__toArray();
     foreach ($extensions as $attributeCode => $attribute) {
         $data[$attributeCode] = $attribute;
     }
     unset($data['extension_attributes']);
     $data['product_id'] = $linkedProduct->getId();
     $links[$linkedProduct->getId()] = $data;
     try {
         $linkTypesToId = $this->linkTypeProvider->getLinkTypes();
         $prodyctHydrator = $this->metadataPool->getHydrator(ProductInterface::class);
         $productData = $prodyctHydrator->extract($product);
         $this->linkResource->saveProductLinks($productData[$this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()], $links, $linkTypesToId[$entity->getLinkType()]);
     } catch (\Exception $exception) {
         throw new CouldNotSaveException(__('Invalid data provided for linked products'));
     }
     return $entity;
 }
 /**
  * Remove grouped products from product relation table
  *
  * @param Link $subject
  * @param \Closure $proceed
  * @param int $linkId
  * @return Link
  */
 public function aroundDeleteProductLink(Link $subject, \Closure $proceed, $linkId)
 {
     /** @var \Magento\Catalog\Model\ProductLink\Link $link */
     $link = $this->linkFactory->create();
     $subject->load($link, $linkId, $subject->getIdFieldName());
     $result = $proceed($linkId);
     if ($link->getLinkTypeId() == \Magento\GroupedProduct\Model\ResourceModel\Product\Link::LINK_TYPE_GROUPED) {
         $this->relationProcessor->removeRelations($link->getProductId(), $link->getLinkedProductId());
     }
     return $result;
 }
Example #3
0
 public function testGetParentIdsByChild()
 {
     $childId = 234;
     $typeId = 4;
     $fetchedData = [['product_id' => 55], ['product_id' => 66]];
     $result = [55, 66];
     // method flow
     $this->prepareAdapter();
     $this->dbSelect->expects($this->once())->method('from')->will($this->returnValue($this->dbSelect));
     $this->dbSelect->expects($this->any())->method('where')->will($this->returnValue($this->dbSelect));
     $this->connection->expects($this->once())->method('fetchAll')->with($this->dbSelect)->will($this->returnValue($fetchedData));
     $this->assertEquals($result, $this->model->getParentIdsByChild($childId, $typeId));
 }
Example #4
0
 /**
  * @param string $entityType
  * @param object $entity
  * @return object
  * @throws CouldNotDeleteException
  * @throws NoSuchEntityException
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function execute($entityType, $entity)
 {
     $linkedProduct = $this->productRepository->get($entity->getLinkedProductSku());
     $product = $this->productRepository->get($entity->getSku());
     $linkTypesToId = $this->linkTypeProvider->getLinkTypes();
     $prodyctHydrator = $this->metadataPool->getHydrator(ProductInterface::class);
     $productData = $prodyctHydrator->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 CouldNotDeleteException(__('Invalid data provided for linked products'));
     }
 }
 protected function processAttributeGetter($dbAttributes)
 {
     $select = $this->getMock('Magento\\Framework\\DB\\Select', [], [], '', false);
     $this->connection->expects($this->once())->method('select')->will($this->returnValue($select));
     $select->expects($this->once())->method('from')->will($this->returnSelf());
     $select->expects($this->once())->method('where')->will($this->returnSelf());
     $this->connection->expects($this->once())->method('fetchAll')->with($select)->will($this->returnValue($dbAttributes));
     $this->link->expects($this->any())->method('getAttributeTypeTable')->will($this->returnValue('table_name'));
 }
Example #6
0
 /**
  * @return array
  */
 public function getAttributes()
 {
     if (empty($this->attributes)) {
         $select = $this->connection->select()->from($this->productLink->getTable('catalog_product_link_attribute'), ['id' => 'product_link_attribute_id', 'code' => 'product_link_attribute_code', 'type' => 'data_type'])->where('link_type_id = ?', $this->getLinkTypeId());
         foreach ($this->connection->fetchAll($select) as $row) {
             $this->attributes[$row['code']] = ['id' => $row['id'], 'table' => $this->productLink->getAttributeTypeTable($row['type'])];
         }
     }
     return $this->attributes;
 }
Example #7
0
 /**
  * Link constructor.
  * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  * @param Relation $catalogProductRelation
  * @param MetadataPool $metadataPool
  * @param string|null $connectionName
  */
 public function __construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, Relation $catalogProductRelation, MetadataPool $metadataPool, $connectionName = null)
 {
     $this->metadataPool = $metadataPool;
     parent::__construct($context, $catalogProductRelation, $connectionName);
 }