示例#1
0
 /**
  * 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->_getWriteAdapter()->delete($this->getMainTable(), $where);
     }
     if (!empty($insert)) {
         $data = [];
         foreach ($insert as $childId) {
             $data[] = ['product_id' => (int) $childId, 'parent_id' => (int) $mainProductId];
         }
         $this->_getWriteAdapter()->insertMultiple($this->getMainTable(), $data);
     }
     // configurable product relations should be added to relation table
     $this->_catalogProductRelation->processRelations($mainProductId, $productIds);
     return $this;
 }
示例#2
0
文件: Bundle.php 项目: aiesh/magento2
 /**
  * Save product relations
  *
  * @param int $parentId
  * @param array $childIds
  * @return $this
  */
 public function saveProductRelations($parentId, $childIds)
 {
     $this->_productRelation->processRelations($parentId, $childIds);
     return $this;
 }