productEntityLinkField) { $this->productEntityLinkField = $this->getMetadataPool() ->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class) ->getLinkField(); } return $this->productEntityLinkField; } public function validateRow(array $rowData, $rowNum) { try { $oldBehavior = $this->getBehavior(); $this->switchBehavior(\Magento\ImportExport\Model\Import::BEHAVIOR_APPEND); return parent::validateRow($rowData, $rowNum); } finally { $this->switchBehavior($oldBehavior); } } protected function getUrlKey($rowData) { if (empty($rowData[self::URL_KEY]) && array_key_exists(self::COL_NAME, $rowData) && $rowData[self::COL_NAME] === null) { return null; } return parent::getUrlKey($rowData); } private function _prepareProductLinks() { $resource = $this->_linkFactory->create(); while ($bunch = $this->_dataSourceModel->getNextBunch()) { $arrEnitityIds = []; $arrLinkNames = $this->_linkNameToId; foreach ($bunch as $rowNum => $rowData) { if (!$this->isRowAllowedToImport($rowData, $rowNum)) { continue; } $sku = $rowData[self::COL_SKU]; $productId = $this->skuProcessor->getNewSku($sku)[$this->getProductEntityLinkField()]; if (!$productId) continue; $arrEnitityIds[] = $productId; foreach ($arrLinkNames as $linkName => $linkId) { if (!array_key_exists($linkName . 'sku', $rowData)) continue; unset($arrLinkNames[$linkName]); } $arrDiff = array_diff_key($this->_linkNameToId, $arrLinkNames); if (array_key_exists('associated_skus', $rowData) && !$rowData['associated_skus']) { $arrDiff['associated_skus'] = \Magento\GroupedProduct\Model\ResourceModel\Product\Link::LINK_TYPE_GROUPED; } if (!empty($arrDiff)) { $this->_connection->delete( $resource->getMainTable(), $this->_connection->quoteInto('product_id IN (?) AND ', array_unique($arrEnitityIds)) . $this->_connection->quoteInto('link_type_id IN (?) ', array_values($arrDiff)) ); } } } return $this; } protected function _saveProductAttributes(array $attributesData) { $linkField = $this->getProductEntityLinkField(); foreach ($attributesData as $tableName => $skuData) { $tableData = []; foreach ($skuData as $sku => $attributes) { $linkId = $this->_oldSku[strtolower($sku)][$linkField]; foreach ($attributes as $attributeId => $storeValues) { foreach ($storeValues as $storeId => $storeValue) { if ($storeValue !== null || $storeId == 0) { if ($storeValue == ' ') $storeValue = ''; $tableData[] = [ $this->getProductEntityLinkField() => $linkId, 'attribute_id' => $attributeId, 'store_id' => $storeId, 'value' => $storeValue, ]; } else { $connection = $this->_connection; $affectedRows = $connection->delete($tableName, array( $this->getProductEntityLinkField() . '=?' => $linkId, 'attribute_id=?' => $attributeId, 'store_id=?' => $storeId, )); } } } } if (!empty($tableData)) { $this->_connection->insertOnDuplicate($tableName, $tableData, ['value']); } } return $this; } protected function getExistingImages($bunch) { return []; } protected function _saveMediaGallery(array $mediaGalleryData) { if (empty($mediaGalleryData)) { return $this; } $cb = function ($data) use (&$cb) { if (!is_array($data)) return false; if (array_key_exists('value', $data)) { return (trim($data['value']) && $data['value'] != '###EMPTY###') ? $data : false; } $data = array_map($cb, $data); return array_filter($data); }; if (\Magento\ImportExport\Model\Import::BEHAVIOR_APPEND != $this->getBehavior()) { $this->initMediaGalleryResources(); $arrSKUS = []; foreach ($mediaGalleryData as $arrStoreData) { $arrSKUS = array_merge($arrSKUS, array_keys((array) $arrStoreData)); } $arrSKUS = array_unique($arrSKUS); $productIds = []; foreach ($arrSKUS as $productSku) { $productId = $this->skuProcessor->getNewSku($productSku)[$this->getProductEntityLinkField()]; $productIds[] = $productId; $this->_connection->delete( $this->mediaGalleryEntityToValueTableName, $this->_connection->quoteInto($this->getProductEntityLinkField() . ' IN (?)', $productId) ); $this->_connection->delete( $this->mediaGalleryValueTableName, $this->_connection->quoteInto($this->getProductEntityLinkField() . ' IN (?)', $productId) ); } } $mediaGalleryData = $cb($mediaGalleryData); $this->switchBehavior(\Magento\ImportExport\Model\Import::BEHAVIOR_APPEND); parent::_saveMediaGallery($mediaGalleryData); $this->resetBehavior(); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $objProductRepository = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface'); $objImageFacory = $objectManager->get('\Magento\Catalog\Model\Product\Image\CacheFactory'); foreach ($productIds as $intProduct) { try { /** @var Product $product */ $product = $objProductRepository->getById($intProduct); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { continue; } $imageCache = $objImageFacory->create(); $imageCache->generate($product); } $sql = 'DELETE FROM ' . $this->mediaGalleryTableName . ' WHERE value_id NOT IN (SELECT value_id FROM ' . $this->mediaGalleryEntityToValueTableName . ')'; $this->_connection->query($sql); return $this; } public function getBehavior() { if (isset($this->_parameters['behavior']) && $this->_parameters['behavior'] == \Magento\ImportExport\Model\Import::BEHAVIOR_ADD_UPDATE) { return $this->_parameters['behavior']; } return parent::getBehavior(); } protected function processRowCategories($rowData) { if (empty($rowData[self::COL_CATEGORY])) { return []; } try { $sep = $this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR]; $this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR] = '###|###'; if (!($this instanceof ProductCategories)) { $arrCategories = explode($this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR], $rowData[self::COL_CATEGORY]); $arrExistingCategories = array_filter($arrCategories, [$this->categoryProcessor, 'getIdFromPath']); if (count($arrCategories) != count($arrExistingCategories)) { $this->errorAggregator->addError( AbstractEntity::ERROR_CODE_CATEGORY_NOT_VALID, ProcessingError::ERROR_LEVEL_NOTICE, $rowData['rowNum'], self::COL_CATEGORY, __('The category import was skipped because not all categories are available yet') ); return []; } } return parent::processRowCategories($rowData); } finally { $this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR] = $sep; } } protected function _saveProductCategories(array $categoriesData) { if ($this->getBehavior() == \Magento\ImportExport\Model\Import::BEHAVIOR_ADD_UPDATE) { $funcRemoveEmpty = function ($varData) use (&$funcRemoveEmpty) { if (is_array($varData)) { return array_filter($varData, $funcRemoveEmpty); } return !empty($varData); }; $arrData = $funcRemoveEmpty($categoriesData); if (empty($arrData)) { return $this; } $categoriesData = $arrData; } return parent::_saveProductCategories($categoriesData); } protected function uploadMediaFiles($fileName, $renameFileOff = false) { if ($fileName === '###EMPTY###') return $fileName; $objUploader = $this->getUploader(); $strDestImagePath = $objUploader->correctFileNameCase($objUploader->getDestDir()); $strSrcImagePath = $objUploader->getTmpDir(); $strBaseFileName = $objUploader->correctFileNameCase($objUploader->getCorrectFileName(basename($fileName))); $strTestFileName = $strBaseFileName; $strDestImagePath .= $objUploader::getDispersionPath($strTestFileName); $fs = \Magento\Framework\App\ObjectManager::getInstance()->create('Magento\Framework\Filesystem'); $directory = $fs->getDirectoryWrite(DirectoryList::ROOT); $strDestImagePathAbs = $directory->getAbsolutePath($strDestImagePath); $strSrcImagePathAbs = $directory->getAbsolutePath($strSrcImagePath); $i = 0; while (file_exists($strDestImagePathAbs . '/' . $strTestFileName)) { if (md5_file($strDestImagePathAbs . '/' . $strTestFileName) == md5_file($strSrcImagePathAbs . '/' . $fileName)) { return $objUploader::getDispersionPath($strTestFileName) . '/' . basename($strTestFileName); } $i++; $strReplace = '_' . strval($i); $strTestFileName = preg_replace('/(\.[^.]+)$/', $strReplace . '\1', $strBaseFileName); } return parent::uploadMediaFiles($fileName); } protected function _saveProducts() { parent::_saveProducts(); $this->_prepareProductLinks(); return $this; $this->switchBehavior(); } protected function _saveLinks() { $this->switchBehavior(); return parent::_saveLinks(); } function switchBehavior($strBehavior = \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND) { if (!$this->strRealBehavior) { $this->strRealBehavior = $this->getBehavior(); } if ($this->strRealBehavior === \Magento\ImportExport\Model\Import::BEHAVIOR_ADD_UPDATE) { $this->_parameters['behavior'] = $strBehavior; } } function resetBehavior() { if ($this->strRealBehavior) { $this->_parameters['behavior'] = $this->strRealBehavior; } } }