/** * {@inheritdoc} */ public function isValid($value) { $this->_clearMessages(); $oldSku = $this->skuProcessor->getOldSkus(); if (!empty($value['_super_products_sku']) && (!isset($oldSku[$value['_super_products_sku']]) && $this->skuProcessor->getNewSku($value['_super_products_sku']) === null)) { $this->_addMessages([self::ERROR_SUPER_PRODUCTS_SKU_NOT_FOUND]); return false; } return true; }
/** * Validate data row. * * @param array $rowData * @param int $rowNum * @return boolean * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function validateRow(array $rowData, $rowNum) { if (isset($this->_validatedRows[$rowNum])) { // check that row is already validated return !$this->getErrorAggregator()->isRowInvalid($rowNum); } $this->_validatedRows[$rowNum] = true; $rowScope = $this->getRowScope($rowData); // BEHAVIOR_DELETE use specific validation logic if (\Magento\ImportExport\Model\Import::BEHAVIOR_DELETE == $this->getBehavior()) { if (self::SCOPE_DEFAULT == $rowScope && !isset($this->_oldSku[$rowData[self::COL_SKU]])) { $this->addRowError(ValidatorInterface::ERROR_SKU_NOT_FOUND_FOR_DELETE, $rowNum); return false; } return true; } if (!$this->validator->isValid($rowData)) { foreach ($this->validator->getMessages() as $message) { $this->addRowError($message, $rowNum); } } $sku = $rowData[self::COL_SKU]; if (null === $sku) { $this->addRowError(ValidatorInterface::ERROR_SKU_IS_EMPTY, $rowNum); } elseif (false === $sku) { $this->addRowError(ValidatorInterface::ERROR_ROW_IS_ORPHAN, $rowNum); } elseif (self::SCOPE_STORE == $rowScope && !$this->storeResolver->getStoreCodeToId($rowData[self::COL_STORE])) { $this->addRowError(ValidatorInterface::ERROR_INVALID_STORE, $rowNum); } // SKU is specified, row is SCOPE_DEFAULT, new product block begins $this->_processedEntitiesCount++; $sku = $rowData[self::COL_SKU]; if (isset($this->_oldSku[$sku])) { // can we get all necessary data from existent DB product? // check for supported type of existing product if (isset($this->_productTypeModels[$this->_oldSku[$sku]['type_id']])) { $this->skuProcessor->addNewSku($sku, ['entity_id' => $this->_oldSku[$sku]['entity_id'], 'type_id' => $this->_oldSku[$sku]['type_id'], 'attr_set_id' => $this->_oldSku[$sku]['attr_set_id'], 'attr_set_code' => $this->_attrSetIdToName[$this->_oldSku[$sku]['attr_set_id']]]); } else { $this->addRowError(ValidatorInterface::ERROR_TYPE_UNSUPPORTED, $rowNum); // child rows of legacy products with unsupported types are orphans $sku = false; } } else { // validate new product type and attribute set if (!isset($rowData[self::COL_TYPE]) || !isset($this->_productTypeModels[$rowData[self::COL_TYPE]])) { $this->addRowError(ValidatorInterface::ERROR_INVALID_TYPE, $rowNum); } elseif (!isset($rowData[self::COL_ATTR_SET]) || !isset($this->_attrSetNameToId[$rowData[self::COL_ATTR_SET]])) { $this->addRowError(ValidatorInterface::ERROR_INVALID_ATTR_SET, $rowNum); } elseif (is_null($this->skuProcessor->getNewSku($sku))) { $this->skuProcessor->addNewSku($sku, ['entity_id' => null, 'type_id' => $rowData[self::COL_TYPE], 'attr_set_id' => $this->_attrSetNameToId[$rowData[self::COL_ATTR_SET]], 'attr_set_code' => $rowData[self::COL_ATTR_SET]]); } if ($this->getErrorAggregator()->isRowInvalid($rowNum)) { // mark SCOPE_DEFAULT row as invalid for future child rows if product not in DB already $sku = false; } } if (!$this->getErrorAggregator()->isRowInvalid($rowNum)) { $newSku = $this->skuProcessor->getNewSku($sku); // set attribute set code into row data for followed attribute validation in type model $rowData[self::COL_ATTR_SET] = $newSku['attr_set_code']; $rowAttributesValid = $this->_productTypeModels[$newSku['type_id']]->isRowValid($rowData, $rowNum, !isset($this->_oldSku[$sku])); if (!$rowAttributesValid && self::SCOPE_DEFAULT == $rowScope) { // mark SCOPE_DEFAULT row as invalid for future child rows if product not in DB already $sku = false; } } // validate custom options $this->getOptionEntity()->validateRow($rowData, $rowNum); return !$this->getErrorAggregator()->isRowInvalid($rowNum); }
/** * Create product model from imported data for URL rewrite purposes. * * @param $rowData * * @return \Magento\Framework\Model\AbstractModel|void * * @throws \Magento\Framework\Exception\LocalizedException */ public function _populateToUrlGeneration($rowData) { $product = $this->catalogProductFactory->create(); $newSku = $this->skuProcessor->getNewSku($rowData[self::COL_SKU]); if (empty($newSku) || !isset($newSku['entity_id'])) { return; } $rowData['entity_id'] = $newSku['entity_id']; $product->addData($rowData); return $product; }
/** * Validate data row. * * @param array $rowData * @param int $rowNum * @return boolean * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function validateRow(array $rowData, $rowNum) { if (isset($this->_validatedRows[$rowNum])) { // check that row is already validated return !$this->getErrorAggregator()->isRowInvalid($rowNum); } $this->_validatedRows[$rowNum] = true; $rowScope = $this->getRowScope($rowData); // BEHAVIOR_DELETE and BEHAVIOR_REPLACE use specific validation logic if (Import::BEHAVIOR_REPLACE == $this->getBehavior()) { if (self::SCOPE_DEFAULT == $rowScope && !isset($this->_oldSku[$rowData[self::COL_SKU]])) { $this->addRowError(ValidatorInterface::ERROR_SKU_NOT_FOUND_FOR_DELETE, $rowNum); return false; } } if (Import::BEHAVIOR_DELETE == $this->getBehavior()) { if (self::SCOPE_DEFAULT == $rowScope && !isset($this->_oldSku[$rowData[self::COL_SKU]])) { $this->addRowError(ValidatorInterface::ERROR_SKU_NOT_FOUND_FOR_DELETE, $rowNum); return false; } return true; } if (!$this->validator->isValid($rowData)) { foreach ($this->validator->getMessages() as $message) { $this->addRowError($message, $rowNum, $this->validator->getInvalidAttribute()); } } $sku = $rowData[self::COL_SKU]; if (null === $sku) { $this->addRowError(ValidatorInterface::ERROR_SKU_IS_EMPTY, $rowNum); } elseif (false === $sku) { $this->addRowError(ValidatorInterface::ERROR_ROW_IS_ORPHAN, $rowNum); } elseif (self::SCOPE_STORE == $rowScope && !$this->storeResolver->getStoreCodeToId($rowData[self::COL_STORE])) { $this->addRowError(ValidatorInterface::ERROR_INVALID_STORE, $rowNum); } // SKU is specified, row is SCOPE_DEFAULT, new product block begins $this->_processedEntitiesCount++; $sku = $rowData[self::COL_SKU]; if (isset($this->_oldSku[$sku])) { // can we get all necessary data from existent DB product? // check for supported type of existing product if (isset($this->_productTypeModels[$this->_oldSku[$sku]['type_id']])) { $this->skuProcessor->addNewSku($sku, $this->prepareNewSkuData($sku)); } else { $this->addRowError(ValidatorInterface::ERROR_TYPE_UNSUPPORTED, $rowNum); // child rows of legacy products with unsupported types are orphans $sku = false; } } else { // validate new product type and attribute set if (!isset($rowData[self::COL_TYPE]) || !isset($this->_productTypeModels[$rowData[self::COL_TYPE]])) { $this->addRowError(ValidatorInterface::ERROR_INVALID_TYPE, $rowNum); } elseif (!isset($rowData[self::COL_ATTR_SET]) || !isset($this->_attrSetNameToId[$rowData[self::COL_ATTR_SET]])) { $this->addRowError(ValidatorInterface::ERROR_INVALID_ATTR_SET, $rowNum); } elseif (is_null($this->skuProcessor->getNewSku($sku))) { $this->skuProcessor->addNewSku($sku, ['row_id' => null, 'entity_id' => null, 'type_id' => $rowData[self::COL_TYPE], 'attr_set_id' => $this->_attrSetNameToId[$rowData[self::COL_ATTR_SET]], 'attr_set_code' => $rowData[self::COL_ATTR_SET]]); } if ($this->getErrorAggregator()->isRowInvalid($rowNum)) { // mark SCOPE_DEFAULT row as invalid for future child rows if product not in DB already $sku = false; } } if (!$this->getErrorAggregator()->isRowInvalid($rowNum)) { $newSku = $this->skuProcessor->getNewSku($sku); // set attribute set code into row data for followed attribute validation in type model $rowData[self::COL_ATTR_SET] = $newSku['attr_set_code']; $rowAttributesValid = $this->_productTypeModels[$newSku['type_id']]->isRowValid($rowData, $rowNum, !isset($this->_oldSku[$sku])); if (!$rowAttributesValid && self::SCOPE_DEFAULT == $rowScope) { // mark SCOPE_DEFAULT row as invalid for future child rows if product not in DB already $sku = false; } } // validate custom options $this->getOptionEntity()->validateRow($rowData, $rowNum); if (!empty($rowData[self::URL_KEY]) || !empty($rowData[self::COL_NAME])) { $urlKey = $this->getUrlKey($rowData); $storeCodes = empty($rowData[self::COL_STORE_VIEW_CODE]) ? array_flip($this->storeResolver->getStoreCodeToId()) : explode($this->getMultipleValueSeparator(), $rowData[self::COL_STORE_VIEW_CODE]); foreach ($storeCodes as $storeCode) { $storeId = $this->storeResolver->getStoreCodeToId($storeCode); $productUrlSuffix = $this->getProductUrlSuffix($storeId); $urlPath = $urlKey . $productUrlSuffix; if (empty($this->urlKeys[$storeId][$urlPath]) || $this->urlKeys[$storeId][$urlPath] == $rowData[self::COL_SKU]) { $this->urlKeys[$storeId][$urlPath] = $rowData[self::COL_SKU]; $this->rowNumbers[$storeId][$urlPath] = $rowNum; } else { $this->addRowError(ValidatorInterface::ERROR_DUPLICATE_URL_KEY, $rowNum); } } } return !$this->getErrorAggregator()->isRowInvalid($rowNum); }
/** * Get array of affected products * * @return int[] */ public function getAffectedEntityIds() { $productIds = []; while ($bunch = $this->_dataSourceModel->getNextBunch()) { foreach ($bunch as $rowNum => $rowData) { if (!$this->isRowAllowedToImport($rowData, $rowNum)) { continue; } $newSku = $this->skuProcessor->getNewSku($rowData[self::COL_SKU]); if (empty($newSku) || !isset($newSku['entity_id'])) { continue; } $productIds[] = $newSku['entity_id']; } } return $productIds; }