|
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,7 +783,9 @@ 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)
|
| 788 | + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
780 | 789 | */
|
781 | 790 | public function __construct(
|
782 | 791 | \Magento\Framework\Json\Helper\Data $jsonHelper,
|
@@ -821,7 +830,8 @@ public function __construct(
|
821 | 830 | ImageTypeProcessor $imageTypeProcessor = null,
|
822 | 831 | MediaGalleryProcessor $mediaProcessor = null,
|
823 | 832 | StockItemImporterInterface $stockItemImporter = null,
|
824 |
| - DateTimeFactory $dateTimeFactory = null |
| 833 | + DateTimeFactory $dateTimeFactory = null, |
| 834 | + ProductRepositoryInterface $productRepository = null |
825 | 835 | ) {
|
826 | 836 | $this->_eventManager = $eventManager;
|
827 | 837 | $this->stockRegistry = $stockRegistry;
|
@@ -875,6 +885,8 @@ public function __construct(
|
875 | 885 | ->initImagesArrayKeys();
|
876 | 886 | $this->validator->init($this);
|
877 | 887 | $this->dateTimeFactory = $dateTimeFactory ?? ObjectManager::getInstance()->get(DateTimeFactory::class);
|
| 888 | + $this->productRepository = $productRepository ?? ObjectManager::getInstance() |
| 889 | + ->get(ProductRepositoryInterface::class); |
878 | 890 | }
|
879 | 891 |
|
880 | 892 | /**
|
@@ -1698,6 +1710,14 @@ protected function _saveProducts()
|
1698 | 1710 | $websiteId = $this->storeResolver->getWebsiteCodeToId($websiteCode);
|
1699 | 1711 | $this->websitesCache[$rowSku][$websiteId] = true;
|
1700 | 1712 | }
|
| 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 | + } |
1701 | 1721 | }
|
1702 | 1722 |
|
1703 | 1723 | // 3. Categories phase
|
@@ -1996,6 +2016,11 @@ protected function processRowCategories($rowData)
|
1996 | 2016 | . ' ' . $error['exception']->getMessage()
|
1997 | 2017 | );
|
1998 | 2018 | }
|
| 2019 | + } else { |
| 2020 | + $product = $this->retrieveProductBySku($rowData['sku']); |
| 2021 | + if ($product) { |
| 2022 | + $categoryIds = $product->getCategoryIds(); |
| 2023 | + } |
1999 | 2024 | }
|
2000 | 2025 | return $categoryIds;
|
2001 | 2026 | }
|
@@ -2998,4 +3023,20 @@ private function formatStockDataForRow(array $rowData): array
|
2998 | 3023 |
|
2999 | 3024 | return $row;
|
3000 | 3025 | }
|
| 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 | + } |
3001 | 3042 | }
|
0 commit comments