Skip to content

Commit 360f428

Browse files
committed
Merge remote-tracking branch 'github-magento2ce/MAGETWO-91544' into EPAM-PR-9
2 parents 6cec515 + 1b6b913 commit 360f428

File tree

1 file changed

+42
-1
lines changed
  • app/code/Magento/CatalogImportExport/Model/Import

1 file changed

+42
-1
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\CatalogImportExport\Model\Import;
77

8+
use Magento\Catalog\Api\ProductRepositoryInterface;
89
use Magento\Catalog\Model\Config as CatalogConfig;
910
use Magento\Catalog\Model\Product\Visibility;
1011
use Magento\CatalogImportExport\Model\Import\Product\MediaGalleryProcessor;
@@ -15,6 +16,7 @@
1516
use Magento\Framework\App\Filesystem\DirectoryList;
1617
use Magento\Framework\App\ObjectManager;
1718
use Magento\Framework\Exception\LocalizedException;
19+
use Magento\Framework\Exception\NoSuchEntityException;
1820
use Magento\Framework\Filesystem;
1921
use Magento\Framework\Intl\DateTimeFactory;
2022
use Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor;
@@ -732,6 +734,11 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
732734
*/
733735
private $dateTimeFactory;
734736

737+
/**
738+
* @var ProductRepositoryInterface
739+
*/
740+
private $productRepository;
741+
735742
/**
736743
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
737744
* @param \Magento\ImportExport\Helper\Data $importExportData
@@ -776,7 +783,9 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
776783
* @param MediaGalleryProcessor $mediaProcessor
777784
* @param StockItemImporterInterface|null $stockItemImporter
778785
* @param DateTimeFactory $dateTimeFactory
786+
* @param ProductRepositoryInterface|null $productRepository
779787
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
788+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
780789
*/
781790
public function __construct(
782791
\Magento\Framework\Json\Helper\Data $jsonHelper,
@@ -821,7 +830,8 @@ public function __construct(
821830
ImageTypeProcessor $imageTypeProcessor = null,
822831
MediaGalleryProcessor $mediaProcessor = null,
823832
StockItemImporterInterface $stockItemImporter = null,
824-
DateTimeFactory $dateTimeFactory = null
833+
DateTimeFactory $dateTimeFactory = null,
834+
ProductRepositoryInterface $productRepository = null
825835
) {
826836
$this->_eventManager = $eventManager;
827837
$this->stockRegistry = $stockRegistry;
@@ -875,6 +885,8 @@ public function __construct(
875885
->initImagesArrayKeys();
876886
$this->validator->init($this);
877887
$this->dateTimeFactory = $dateTimeFactory ?? ObjectManager::getInstance()->get(DateTimeFactory::class);
888+
$this->productRepository = $productRepository ?? ObjectManager::getInstance()
889+
->get(ProductRepositoryInterface::class);
878890
}
879891

880892
/**
@@ -1698,6 +1710,14 @@ protected function _saveProducts()
16981710
$websiteId = $this->storeResolver->getWebsiteCodeToId($websiteCode);
16991711
$this->websitesCache[$rowSku][$websiteId] = true;
17001712
}
1713+
} else {
1714+
$product = $this->retrieveProductBySku($rowSku);
1715+
if ($product) {
1716+
$websiteIds = $product->getWebsiteIds();
1717+
foreach ($websiteIds as $websiteId) {
1718+
$this->websitesCache[$rowSku][$websiteId] = true;
1719+
}
1720+
}
17011721
}
17021722

17031723
// 3. Categories phase
@@ -1996,6 +2016,11 @@ protected function processRowCategories($rowData)
19962016
. ' ' . $error['exception']->getMessage()
19972017
);
19982018
}
2019+
} else {
2020+
$product = $this->retrieveProductBySku($rowData['sku']);
2021+
if ($product) {
2022+
$categoryIds = $product->getCategoryIds();
2023+
}
19992024
}
20002025
return $categoryIds;
20012026
}
@@ -2998,4 +3023,20 @@ private function formatStockDataForRow(array $rowData): array
29983023

29993024
return $row;
30003025
}
3026+
3027+
/**
3028+
* Retrieve product by sku.
3029+
*
3030+
* @param string $sku
3031+
* @return \Magento\Catalog\Api\Data\ProductInterface|null
3032+
*/
3033+
private function retrieveProductBySku($sku)
3034+
{
3035+
try {
3036+
$product = $this->productRepository->get($sku);
3037+
} catch (NoSuchEntityException $e) {
3038+
return null;
3039+
}
3040+
return $product;
3041+
}
30013042
}

0 commit comments

Comments
 (0)