/**
  * 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;
 }
Example #2
0
 /**
  * @param Product $object
  * @return Product
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function afterSave($object)
 {
     if ($object->getIsDuplicate() == true) {
         $this->duplicate($object, $this->getAttribute());
         return $object;
     }
     $attrCode = $this->getAttribute()->getAttributeCode();
     $value = $object->getData($attrCode);
     if (!is_array($value) || !isset($value['images']) || $object->isLockedAttribute($attrCode)) {
         return $object;
     }
     $storeId = $object->getStoreId();
     $storeIds = $object->getStoreIds();
     $storeIds[] = \Magento\Store\Model\Store::DEFAULT_STORE_ID;
     // remove current storeId
     $storeIds = array_flip($storeIds);
     unset($storeIds[$storeId]);
     $storeIds = array_keys($storeIds);
     $images = $this->productFactory->create()->getAssignedImages($object, $storeIds);
     $picturesInOtherStores = [];
     foreach ($images as $image) {
         $picturesInOtherStores[$image['filepath']] = true;
     }
     $recordsToDelete = [];
     $filesToDelete = [];
     foreach ($value['images'] as &$image) {
         if (!empty($image['removed'])) {
             if (!empty($image['value_id']) && !isset($picturesInOtherStores[$image['file']])) {
                 $recordsToDelete[] = $image['value_id'];
                 $filesToDelete[] = ltrim($image['file'], '/');
             }
             continue;
         }
         if (empty($image['value_id'])) {
             $data = [];
             $data['attribute_id'] = $this->getAttribute()->getId();
             $data['value'] = $image['file'];
             if (!empty($image['media_type'])) {
                 $data['media_type'] = $image['media_type'];
             }
             $image['value_id'] = $this->getResource()->insertGallery($data);
             $this->getResource()->bindValueToEntity($image['value_id'], $object->getId());
         }
         $this->getResource()->deleteGalleryValueInStore($image['value_id'], $object->getId(), $object->getStoreId());
         // Add per store labels, position, disabled
         $data = [];
         $data['value_id'] = $image['value_id'];
         $data['entity_id'] = $object->getId();
         $data['label'] = isset($image['label']) ? $image['label'] : '';
         $data['position'] = isset($image['position']) ? (int) $image['position'] : 0;
         $data['disabled'] = isset($image['disabled']) ? (int) $image['disabled'] : 0;
         $data['store_id'] = (int) $object->getStoreId();
         $data['entity_id'] = (int) $object->getId();
         $this->getResource()->insertGalleryValueInStore($data);
     }
     $this->getResource()->deleteGallery($recordsToDelete);
     $this->removeDeletedImages($filesToDelete);
     $object->setData($attrCode, $value);
     return $object;
 }
 /**
  * @param string $entityType
  * @param \Magento\Catalog\Model\Product $product
  * @return \Magento\Catalog\Model\Product
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute($entityType, $product)
 {
     $attrCode = $this->getAttribute()->getAttributeCode();
     $value = $product->getData($attrCode);
     if (!is_array($value) || !isset($value['images'])) {
         return $product;
     }
     if (!is_array($value['images']) && strlen($value['images']) > 0) {
         $value['images'] = $this->jsonHelper->jsonDecode($value['images']);
     }
     if (!is_array($value['images'])) {
         $value['images'] = [];
     }
     $clearImages = [];
     $newImages = [];
     $existImages = [];
     if ($product->getIsDuplicate() != true) {
         foreach ($value['images'] as &$image) {
             if (!empty($image['removed'])) {
                 $clearImages[] = $image['file'];
             } elseif (empty($image['value_id'])) {
                 $newFile = $this->moveImageFromTmp($image['file']);
                 $image['new_file'] = $newFile;
                 $newImages[$image['file']] = $image;
                 $image['file'] = $newFile;
             } else {
                 $existImages[$image['file']] = $image;
             }
         }
     } else {
         // For duplicating we need copy original images.
         $duplicate = [];
         foreach ($value['images'] as &$image) {
             if (empty($image['value_id']) || !empty($image['removed'])) {
                 continue;
             }
             $duplicate[$image['value_id']] = $this->copyImage($image['file']);
             $image['new_file'] = $duplicate[$image['value_id']];
             $newImages[$image['file']] = $image;
         }
         $value['duplicate'] = $duplicate;
     }
     /* @var $mediaAttribute \Magento\Catalog\Api\Data\ProductAttributeInterface */
     foreach ($product->getMediaAttributes() as $mediaAttribute) {
         $mediaAttrCode = $mediaAttribute->getAttributeCode();
         $attrData = $product->getData($mediaAttrCode);
         if (in_array($attrData, $clearImages)) {
             $product->setData($mediaAttrCode, 'no_selection');
         }
         if (in_array($attrData, array_keys($newImages))) {
             $product->setData($mediaAttrCode, $newImages[$attrData]['new_file']);
             $product->setData($mediaAttrCode . '_label', $newImages[$attrData]['label']);
         }
         if (in_array($attrData, array_keys($existImages))) {
             $product->setData($mediaAttrCode . '_label', $existImages[$attrData]['label']);
         }
         $product->addAttributeUpdate($mediaAttrCode, $product->getData($mediaAttrCode), $product->getStoreId());
     }
     $product->setData($attrCode, $value);
     if ($product->getIsDuplicate() == true) {
         $this->duplicate($product);
         return $product;
     }
     if (!is_array($value) || !isset($value['images']) || $product->isLockedAttribute($attrCode)) {
         return $product;
     }
     $this->processDeletedImages($product, $value['images']);
     $this->processNewAndExistingImages($product, $value['images']);
     $product->setData($attrCode, $value);
     return $product;
 }