/** * Save configurable product relations * * @param \Magento\Catalog\Model\Product $mainProduct the parent id * @param array $productIds the children id array * @return $this */ public function saveProducts($mainProduct, $productIds) { $isProductInstance = false; if ($mainProduct instanceof \Magento\Catalog\Model\Product) { $mainProductId = $mainProduct->getId(); $isProductInstance = true; } $old = []; if (!$mainProduct->getIsDuplicate()) { $old = $mainProduct->getTypeInstance()->getUsedProductIds($mainProduct); } $insert = array_diff($productIds, $old); $delete = array_diff($old, $productIds); if ((!empty($insert) || !empty($delete)) && $isProductInstance) { $mainProduct->setIsRelationsChanged(true); } if (!empty($delete)) { $where = ['parent_id = ?' => $mainProductId, 'product_id IN(?)' => $delete]; $this->getConnection()->delete($this->getMainTable(), $where); } if (!empty($insert)) { $data = []; foreach ($insert as $childId) { $data[] = ['product_id' => (int) $childId, 'parent_id' => (int) $mainProductId]; } $this->getConnection()->insertMultiple($this->getMainTable(), $data); } // configurable product relations should be added to relation table $this->_catalogProductRelation->processRelations($mainProductId, $productIds); return $this; }
/** * {@inheritdoc} */ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); if (version_compare($context->getVersion(), '2.0.1', '<')) { $connection = $setup->getConnection(); $select = $connection->select()->from($this->relationProcessor->getTable('catalog_product_link'), ['product_id', 'linked_product_id'])->where('link_type_id = ?', Link::LINK_TYPE_GROUPED); $connection->query($connection->insertFromSelect($select, $this->relationProcessor->getMainTable(), ['parent_id', 'child_id'], AdapterInterface::INSERT_IGNORE)); } $setup->endSetup(); }
/** * 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; }
public function testAroundDeleteProductLink() { $subject = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Link::class)->disableOriginalConstructor()->getMock(); $subject->expects($this->any())->method('getIdFieldName')->willReturn('id'); $subject->expects($this->once())->method('load')->with($this->link, 155, 'id'); $this->link->expects($this->any())->method('getLinkTypeId')->willReturn(\Magento\GroupedProduct\Model\ResourceModel\Product\Link::LINK_TYPE_GROUPED); $this->link->expects($this->any())->method('getProductId')->willReturn(12); $this->link->expects($this->any())->method('getLinkedProductId')->willReturn(13); $this->relationProcessor->expects($this->once())->method('removeRelations')->with(12, 13); $this->assertEquals($subject, $this->object->aroundDeleteProductLink($subject, function () use($subject) { return $subject; }, 155)); }
/** * Save configurable product relations * * @param \Magento\Catalog\Model\Product $mainProduct the parent id * @param array $productIds the children id array * @return $this */ public function saveProducts($mainProduct, array $productIds) { if (!$mainProduct instanceof ProductInterface) { return $this; } $productId = $mainProduct->getData($this->getProductEntityLinkField()); $data = []; foreach ($productIds as $id) { $data[] = ['product_id' => (int) $id, 'parent_id' => (int) $productId]; } if (!empty($data)) { $this->getConnection()->insertOnDuplicate($this->getMainTable(), $data, ['product_id', 'parent_id']); } $where = ['parent_id = ?' => $productId]; if (!empty($productIds)) { $where['product_id NOT IN(?)'] = $productIds; } $this->getConnection()->delete($this->getMainTable(), $where); // configurable product relations should be added to relation table $this->catalogProductRelation->processRelations($productId, $productIds); return $this; }
/** * Remove product relations * * @param int $parentId * @param array $childIds * @return $this */ public function removeProductRelations($parentId, $childIds) { $this->_productRelation->removeRelations($parentId, $childIds); return $this; }
/** * Save product relations * * @param int $parentId * @param array $childIds * @return $this */ public function saveProductRelations($parentId, $childIds) { $this->_productRelation->processRelations($parentId, $childIds); return $this; }