|
5 | 5 | */
|
6 | 6 | namespace Magento\CatalogImportExport\Model\Import;
|
7 | 7 |
|
| 8 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
8 | 9 | use Magento\Catalog\Model\Config as CatalogConfig;
|
9 | 10 | use Magento\Catalog\Model\Product\Visibility;
|
10 | 11 | use Magento\CatalogImportExport\Model\Import\Product\MediaGalleryProcessor;
|
|
15 | 16 | use Magento\Framework\App\Filesystem\DirectoryList;
|
16 | 17 | use Magento\Framework\App\ObjectManager;
|
17 | 18 | use Magento\Framework\Exception\LocalizedException;
|
| 19 | +use Magento\Framework\Exception\NoSuchEntityException; |
18 | 20 | use Magento\Framework\Filesystem;
|
19 | 21 | use Magento\Framework\Intl\DateTimeFactory;
|
20 | 22 | use Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor;
|
@@ -732,6 +734,11 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
|
732 | 734 | */
|
733 | 735 | private $dateTimeFactory;
|
734 | 736 |
|
| 737 | + /** |
| 738 | + * @var ProductRepositoryInterface |
| 739 | + */ |
| 740 | + private $productRepository; |
| 741 | + |
735 | 742 | /**
|
736 | 743 | * @param \Magento\Framework\Json\Helper\Data $jsonHelper
|
737 | 744 | * @param \Magento\ImportExport\Helper\Data $importExportData
|
@@ -776,6 +783,7 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
|
776 | 783 | * @param MediaGalleryProcessor $mediaProcessor
|
777 | 784 | * @param StockItemImporterInterface|null $stockItemImporter
|
778 | 785 | * @param DateTimeFactory $dateTimeFactory
|
| 786 | + * @param ProductRepositoryInterface|null $productRepository |
779 | 787 | * @SuppressWarnings(PHPMD.ExcessiveParameterList)
|
780 | 788 | */
|
781 | 789 | public function __construct(
|
@@ -821,7 +829,8 @@ public function __construct(
|
821 | 829 | ImageTypeProcessor $imageTypeProcessor = null,
|
822 | 830 | MediaGalleryProcessor $mediaProcessor = null,
|
823 | 831 | StockItemImporterInterface $stockItemImporter = null,
|
824 |
| - DateTimeFactory $dateTimeFactory = null |
| 832 | + DateTimeFactory $dateTimeFactory = null, |
| 833 | + ProductRepositoryInterface $productRepository = null |
825 | 834 | ) {
|
826 | 835 | $this->_eventManager = $eventManager;
|
827 | 836 | $this->stockRegistry = $stockRegistry;
|
@@ -875,6 +884,8 @@ public function __construct(
|
875 | 884 | ->initImagesArrayKeys();
|
876 | 885 | $this->validator->init($this);
|
877 | 886 | $this->dateTimeFactory = $dateTimeFactory ?? ObjectManager::getInstance()->get(DateTimeFactory::class);
|
| 887 | + $this->productRepository = $productRepository ?? ObjectManager::getInstance() |
| 888 | + ->get(ProductRepositoryInterface::class); |
878 | 889 | }
|
879 | 890 |
|
880 | 891 | /**
|
@@ -1687,6 +1698,14 @@ protected function _saveProducts()
|
1687 | 1698 | $websiteId = $this->storeResolver->getWebsiteCodeToId($websiteCode);
|
1688 | 1699 | $this->websitesCache[$rowSku][$websiteId] = true;
|
1689 | 1700 | }
|
| 1701 | + } else { |
| 1702 | + $product = $this->retrieveProductBySku($rowSku); |
| 1703 | + if ($product) { |
| 1704 | + $websiteIds = $product->getWebsiteIds(); |
| 1705 | + foreach ($websiteIds as $websiteId) { |
| 1706 | + $this->websitesCache[$rowSku][$websiteId] = true; |
| 1707 | + } |
| 1708 | + } |
1690 | 1709 | }
|
1691 | 1710 |
|
1692 | 1711 | // 3. Categories phase
|
@@ -1941,6 +1960,11 @@ protected function processRowCategories($rowData)
|
1941 | 1960 | . ' ' . $error['exception']->getMessage()
|
1942 | 1961 | );
|
1943 | 1962 | }
|
| 1963 | + } else { |
| 1964 | + $product = $this->retrieveProductBySku($rowData['sku']); |
| 1965 | + if ($product) { |
| 1966 | + $categoryIds = $product->getCategoryIds(); |
| 1967 | + } |
1944 | 1968 | }
|
1945 | 1969 | return $categoryIds;
|
1946 | 1970 | }
|
@@ -2911,4 +2935,20 @@ private function formatStockDataForRow(array $rowData): array
|
2911 | 2935 |
|
2912 | 2936 | return $row;
|
2913 | 2937 | }
|
| 2938 | + |
| 2939 | + /** |
| 2940 | + * Retrieve product by sku. |
| 2941 | + * |
| 2942 | + * @param $sku |
| 2943 | + * @return \Magento\Catalog\Api\Data\ProductInterface|null |
| 2944 | + */ |
| 2945 | + private function retrieveProductBySku($sku) |
| 2946 | + { |
| 2947 | + try { |
| 2948 | + $product = $this->productRepository->get($sku); |
| 2949 | + } catch (NoSuchEntityException $e) { |
| 2950 | + return null; |
| 2951 | + } |
| 2952 | + return $product; |
| 2953 | + } |
2914 | 2954 | }
|
0 commit comments