/**
  * 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;
 }
 /**
  * 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;
 }
Beispiel #3
0
 /**
  * Save product relations
  *
  * @param int $parentId
  * @param array $childIds
  * @return $this
  */
 public function saveProductRelations($parentId, $childIds)
 {
     $this->_productRelation->processRelations($parentId, $childIds);
     return $this;
 }