I want to override
\Magento\ImportExport\Model\ResourceModel\Import\Data
public function getNextBunch()
{
if (null === $this->_iterator) {
$this->_iterator = $this->getIterator();
$this->_iterator->rewind();
}
$dataRow = null;
if ($this->_iterator->valid()) {
$encodedData = $this->_iterator->current();
if (array_key_exists(0, $encodedData) && $encodedData[0]) {
$dataRow = $this->jsonHelper->jsonDecode($encodedData[0]);
$this->_iterator->next();
}
}
if (!$dataRow) {
$this->_iterator = null;
}
if( is_array($dataRow) || $dataRow instanceof \Traversable || $dataRow instanceof \Iterator || $dataRow instanceof \IteratorAggregate){
foreach($dataRow as $dataRows)
{
$dataRows[\Magento\CatalogImportExport\Model\Import\Product::COL_TYPE]="downloadable";
$dataRows[\Magento\CatalogImportExport\Model\Import\Product::COL_ATTR_SET]="Migration_Default";
$dataRows[self::COL_ATTR_SET]="Migration_Default";
$this->dataRowNew[]=$dataRows;
}
}
$dataRow=$this->dataRowNew;
echo "<pre>";
var_dump($dataRow);
die("<br> in".__FILE__." : ".__LINE__);
return $dataRow;
}
But foreach not working it stops the product import flow. Can anyone help?
getNextBunch() function called in \Magento\CatalogImportExport\Model\Import\Product when i removed foreach loop it's works but in foreach loop it goes in infinite stage.
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
$entityRowsIn = [];
$entityRowsUp = [];
$attributes = [];
$this->websitesCache = [];
$this->categoriesCache = [];
$tierPrices = [];
$mediaGallery = [];
$labelsForUpdate = [];
$uploadedImages = [];
$previousType = null;
$prevAttributeSet = null;
$existingImages = $this->getExistingImages($bunch);
foreach ($bunch as $rowNum => $rowData) {
$sku=$rowData[self::COL_SKU];
$importStartTime[$sku] = date("Y-m-d H:i:s");
// reset category processor's failed categories array
$this->categoryProcessor->clearFailedCategories();
if (!$this->validateRow($rowData, $rowNum)) {
continue;
}
if ($this->getErrorAggregator()->hasToBeTerminated()) {
$this->getErrorAggregator()->addRowToSkip($rowNum);
continue;
}
$rowScope = $this->getRowScope($rowData);
$rowData[self::URL_KEY] = $this->getUrlKey($rowData);
$rowSku = $rowData[self::COL_SKU];
if (null === $rowSku) {
$this->getErrorAggregator()->addRowToSkip($rowNum);
continue;
} elseif (self::SCOPE_STORE == $rowScope) {
// set necessary data from SCOPE_DEFAULT row
$rowData[self::COL_TYPE] = $this->skuProcessor->getNewSku($rowSku)['type_id'];
$rowData['attribute_set_id'] = $this->skuProcessor->getNewSku($rowSku)['attr_set_id'];
$rowData[self::COL_ATTR_SET] = $this->skuProcessor->getNewSku($rowSku)['attr_set_code'];
}
// 1. Entity phase
if ($this->isSkuExist($rowSku)) {
// existing row
if (isset($rowData['attribute_set_code'])) {
$attributeSetId = $this->catalogConfig->getAttributeSetId(
$this->getEntityTypeId(),
$rowData['attribute_set_code']
);
// wrong attribute_set_code was received
if (!$attributeSetId) {
throw new \Magento\Framework\Exception\LocalizedException(
__(
'Wrong attribute set code "%1", please correct it and try again.',
$rowData['attribute_set_code']
)
);
}
} else {
$attributeSetId = $this->skuProcessor->getNewSku($rowSku)['attr_set_id'];
}
$entityRowsUp[] = [
'updated_at' => (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT),
'attribute_set_id' => $attributeSetId,
$entityLinkField => $this->getExistingSku($rowSku)[$entityLinkField]
];
} else {
if (!$productLimit || $productsQty < $productLimit) {
$entityRowsIn[strtolower($rowSku)] = [
'attribute_set_id' => $this->skuProcessor->getNewSku($rowSku)['attr_set_id'],
'type_id' => $this->skuProcessor->getNewSku($rowSku)['type_id'],
'sku' => $rowSku,
'has_options' => isset($rowData['has_options']) ? $rowData['has_options'] : 0,
'created_at' => (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT),
'updated_at' => (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT),
];
$productsQty++;
} else {
$rowSku = null;
// sign for child rows to be skipped
$this->getErrorAggregator()->addRowToSkip($rowNum);
continue;
}
}
if (!array_key_exists($rowSku, $this->websitesCache)) {
$this->websitesCache[$rowSku] = [];
}
// 2. Product-to-Website phase
$rowData[self::COL_PRODUCT_WEBSITES]="base";
if (!empty($rowData[self::COL_PRODUCT_WEBSITES])) {
$websiteCodes = explode($this->getMultipleValueSeparator(), $rowData[self::COL_PRODUCT_WEBSITES]);
foreach ($websiteCodes as $websiteCode) {
$websiteId = $this->storeResolver->getWebsiteCodeToId($websiteCode);
$this->websitesCache[$rowSku][$websiteId] = true;
}
}
// 3. Categories phase
if (!array_key_exists($rowSku, $this->categoriesCache)) {
$this->categoriesCache[$rowSku] = [];
}
$rowData['rowNum'] = $rowNum;
$categoryIds = $this->processRowCategories($rowData);
foreach ($categoryIds as $id) {
$this->categoriesCache[$rowSku][$id] = true;
}
unset($rowData['rowNum']);
// 4.1. Tier prices phase
if (!empty($rowData['_tier_price_website'])) {
$tierPrices[$rowSku][] = [
'all_groups' => $rowData['_tier_price_customer_group'] == self::VALUE_ALL,
'customer_group_id' => $rowData['_tier_price_customer_group'] ==
self::VALUE_ALL ? 0 : $rowData['_tier_price_customer_group'],
'qty' => $rowData['_tier_price_qty'],
'value' => $rowData['_tier_price_price'],
'website_id' => self::VALUE_ALL == $rowData['_tier_price_website'] ||
$priceIsGlobal ? 0 : $this->storeResolver->getWebsiteCodeToId($rowData['_tier_price_website']),
];
}
if (!$this->validateRow($rowData, $rowNum)) {
continue;
}
// 5. Media gallery phase
$disabledImages = [];
list($rowImages, $rowLabels) = $this->getImagesFromRow($rowData);
if (isset($rowData['_media_is_disabled'])) {
$disabledImages = array_flip(
explode($this->getMultipleValueSeparator(), $rowData['_media_is_disabled'])
);
}
$rowData[self::COL_MEDIA_IMAGE] = [];
/*
* Note: to avoid problems with undefined sorting, the value of media gallery items positions
* must be unique in scope of one product.
*/
$position = 0;
foreach ($rowImages as $column => $columnImages) {
foreach ($columnImages as $columnImageKey => $columnImage) {
if (!isset($uploadedImages[$columnImage])) {
$uploadedFile = $this->uploadMediaFiles($columnImage, true);
$uploadedFile = $uploadedFile ?: $this->getSystemFile($columnImage);
if ($uploadedFile) {
$uploadedImages[$columnImage] = $uploadedFile;
} else {
$this->addRowError(
ValidatorInterface::ERROR_MEDIA_URL_NOT_ACCESSIBLE,
$rowNum,
null,
null,
ProcessingError::ERROR_LEVEL_NOT_CRITICAL
);
}
} else {
$uploadedFile = $uploadedImages[$columnImage];
}
if ($uploadedFile && $column !== self::COL_MEDIA_IMAGE) {
$rowData[$column] = $uploadedFile;
}
if ($uploadedFile && !isset($mediaGallery[$rowSku][$uploadedFile])) {
if (isset($existingImages[$rowSku][$uploadedFile])) {
if (isset($rowLabels[$column][$columnImageKey])
&& $rowLabels[$column][$columnImageKey] != $existingImages[$rowSku][$uploadedFile]['label']
) {
$labelsForUpdate[] = [
'label' => $rowLabels[$column][$columnImageKey],
'imageData' => $existingImages[$rowSku][$uploadedFile]
];
}
} else {
if ($column == self::COL_MEDIA_IMAGE) {
$rowData[$column][] = $uploadedFile;
}
$mediaGallery[$rowSku][$uploadedFile] = [
'attribute_id' => $this->getMediaGalleryAttributeId(),
'label' => isset($rowLabels[$column][$columnImageKey]) ? $rowLabels[$column][$columnImageKey] : '',
'position' => ++$position,
'disabled' => isset($disabledImages[$columnImage]) ? '1' : '0',
'value' => $uploadedFile,
];
}
}
}
}
// 6. Attributes phase
$rowStore = (self::SCOPE_STORE == $rowScope)
? $this->storeResolver->getStoreCodeToId($rowData[self::COL_STORE])
: 0;
$productType = isset($rowData[self::COL_TYPE]) ? $rowData[self::COL_TYPE] : null;
if (!is_null($productType)) {
$previousType = $productType;
}
if (isset($rowData[self::COL_ATTR_SET])) {
$prevAttributeSet = $rowData[self::COL_ATTR_SET];
}
if (self::SCOPE_NULL == $rowScope) {
// for multiselect attributes only
if (!is_null($prevAttributeSet)) {
$rowData[self::COL_ATTR_SET] = $prevAttributeSet;
}
if (is_null($productType) && !is_null($previousType)) {
$productType = $previousType;
}
if (is_null($productType)) {
continue;
}
}
$productTypeModel = $this->_productTypeModels[$productType];
if (!empty($rowData['tax_class_name'])) {
$rowData['tax_class_id'] =
$this->taxClassProcessor->upsertTaxClass($rowData['tax_class_name'], $productTypeModel);
}
if ($this->getBehavior() == Import::BEHAVIOR_APPEND ||
empty($rowData[self::COL_SKU])
) {
$rowData = $productTypeModel->clearEmptyData($rowData);
}
$rowData = $productTypeModel->prepareAttributesWithDefaultValueForSave(
$rowData,
!$this->isSkuExist($rowSku)
);
$product = $this->_proxyProdFactory->create(['data' => $rowData]);
foreach ($rowData as $attrCode => $attrValue) {
$attribute = $this->retrieveAttributeByCode($attrCode);
if ('multiselect' != $attribute->getFrontendInput() && self::SCOPE_NULL == $rowScope) {
// skip attribute processing for SCOPE_NULL rows
continue;
}
$attrId = $attribute->getId();
$backModel = $attribute->getBackendModel();
$attrTable = $attribute->getBackend()->getTable();
$storeIds = [0];
if (
'datetime' == $attribute->getBackendType()
&& (
in_array($attribute->getAttributeCode(), $this->dateAttrCodes)
|| $attribute->getIsUserDefined()
)
) {
$attrValue = $this->dateTime->formatDate($attrValue, false);
} elseif ('datetime' == $attribute->getBackendType() && strtotime($attrValue)) {
$attrValue = $this->dateTime->gmDate(
'Y-m-d H:i:s',
$this->_localeDate->date($attrValue)->getTimestamp()
);
} elseif ($backModel) {
$attribute->getBackend()->beforeSave($product);
$attrValue = $product->getData($attribute->getAttributeCode());
}
if (self::SCOPE_STORE == $rowScope) {
if (self::SCOPE_WEBSITE == $attribute->getIsGlobal()) {
// check website defaults already set
if (!isset($attributes[$attrTable][$rowSku][$attrId][$rowStore])) {
$storeIds = $this->storeResolver->getStoreIdToWebsiteStoreIds($rowStore);
}
} elseif (self::SCOPE_STORE == $attribute->getIsGlobal()) {
$storeIds = [$rowStore];
}
if (!$this->isSkuExist($rowSku)) {
$storeIds[] = 0;
}
}
foreach ($storeIds as $storeId) {
if (!isset($attributes[$attrTable][$rowSku][$attrId][$storeId])) {
$attributes[$attrTable][$rowSku][$attrId][$storeId] = $attrValue;
}
}
// restore 'backend_model' to avoid 'default' setting
$attribute->setBackendModel($backModel);
}
$importEndTime[$rowSku] = date("Y-m-d H:i:s");
}
$errorRow=[];
foreach ($bunch as $rowNum => $rowData) {
if ($this->getErrorAggregator()->isRowInvalid($rowNum)) {
$errorRow[$rowSku][]=$rowData[$rowSku];
$errorRow[$rowSku]['error']=$this->getErrorAggregator()->getErrorByRowNumber($rowNum);
unset($bunch[$rowNum]);
}
}
$this->saveProductEntity(
$entityRowsIn,
$entityRowsUp
)->_saveProductWebsites(
$this->websitesCache
)->_saveProductCategories(
$this->categoriesCache
)->_saveProductTierPrices(
$tierPrices
)->_saveMediaGallery(
$mediaGallery
)->_saveProductAttributes(
$attributes
)->updateMediaGalleryLabels(
$labelsForUpdate
);
$importData['importstarttime']=$importStartTime;
$importData['importendtime']=$importEndTime;
$importData['errorData']= $errorRow;
$this->_eventManager->dispatch(
'catalog_product_import_bunch_save_after',
['adapter' => $this, 'bunch' => $bunch]
);
$this->_eventManager->dispatch(
'catalog_product_import_log_save_after',
['adapter' => $this, 'bunch' => $bunch,'importData'=>$importData]
);
}