Skip to content

Commit 5da1c35

Browse files
MAGETWO-91544: Updated url keys of product after import produce 404 error
- Add website ids if this column doesn't exist for url rewrite - Add category ids if this column doesn't exist for url rewrite
1 parent 3e3c5f1 commit 5da1c35

File tree

1 file changed

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

1 file changed

+41
-1
lines changed

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

Lines changed: 41 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,6 +783,7 @@ 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)
780788
*/
781789
public function __construct(
@@ -821,7 +829,8 @@ public function __construct(
821829
ImageTypeProcessor $imageTypeProcessor = null,
822830
MediaGalleryProcessor $mediaProcessor = null,
823831
StockItemImporterInterface $stockItemImporter = null,
824-
DateTimeFactory $dateTimeFactory = null
832+
DateTimeFactory $dateTimeFactory = null,
833+
ProductRepositoryInterface $productRepository = null
825834
) {
826835
$this->_eventManager = $eventManager;
827836
$this->stockRegistry = $stockRegistry;
@@ -875,6 +884,8 @@ public function __construct(
875884
->initImagesArrayKeys();
876885
$this->validator->init($this);
877886
$this->dateTimeFactory = $dateTimeFactory ?? ObjectManager::getInstance()->get(DateTimeFactory::class);
887+
$this->productRepository = $productRepository ?? ObjectManager::getInstance()
888+
->get(ProductRepositoryInterface::class);
878889
}
879890

880891
/**
@@ -1687,6 +1698,14 @@ protected function _saveProducts()
16871698
$websiteId = $this->storeResolver->getWebsiteCodeToId($websiteCode);
16881699
$this->websitesCache[$rowSku][$websiteId] = true;
16891700
}
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+
}
16901709
}
16911710

16921711
// 3. Categories phase
@@ -1941,6 +1960,11 @@ protected function processRowCategories($rowData)
19411960
. ' ' . $error['exception']->getMessage()
19421961
);
19431962
}
1963+
} else {
1964+
$product = $this->retrieveProductBySku($rowData['sku']);
1965+
if ($product) {
1966+
$categoryIds = $product->getCategoryIds();
1967+
}
19441968
}
19451969
return $categoryIds;
19461970
}
@@ -2911,4 +2935,20 @@ private function formatStockDataForRow(array $rowData): array
29112935

29122936
return $row;
29132937
}
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+
}
29142954
}

0 commit comments

Comments
 (0)