Skip to content

Commit 7140e99

Browse files
merge magento/2.3-develop into magento-epam/EPAM-PR-77
2 parents d72f87f + ff2fa21 commit 7140e99

File tree

8 files changed

+241
-106
lines changed

8 files changed

+241
-106
lines changed

app/code/Magento/Catalog/Model/Indexer/Product/Category/Action/Rows.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function execute(array $entityIds = [], $useTempTable = false)
105105
* @throws \Exception if metadataPool doesn't contain metadata for ProductInterface
106106
* @throws \DomainException
107107
*/
108-
private function getProductIdsWithParents(array $childProductIds)
108+
private function getProductIdsWithParents(array $childProductIds): array
109109
{
110110
/** @var \Magento\Framework\EntityManager\EntityMetadataInterface $metadata */
111111
$metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
@@ -123,8 +123,12 @@ private function getProductIdsWithParents(array $childProductIds)
123123
);
124124

125125
$parentProductIds = $this->connection->fetchCol($select);
126+
$ids = array_unique(array_merge($childProductIds, $parentProductIds));
127+
foreach ($ids as $key => $id) {
128+
$ids[$key] = (int) $id;
129+
}
126130

127-
return array_unique(array_merge($childProductIds, $parentProductIds));
131+
return $ids;
128132
}
129133

130134
/**
@@ -175,7 +179,7 @@ protected function removeEntries()
175179
protected function getNonAnchorCategoriesSelect(\Magento\Store\Model\Store $store)
176180
{
177181
$select = parent::getNonAnchorCategoriesSelect($store);
178-
return $select->where('ccp.product_id IN (?) OR relation.child_id IN (?)', $this->limitationByProducts);
182+
return $select->where('ccp.product_id IN (?)', $this->limitationByProducts);
179183
}
180184

181185
/**
@@ -216,28 +220,28 @@ protected function isRangingNeeded()
216220
* Returns a list of category ids which are assigned to product ids in the index
217221
*
218222
* @param array $productIds
219-
* @return \Magento\Framework\Indexer\CacheContext
223+
* @return array
220224
*/
221-
private function getCategoryIdsFromIndex(array $productIds)
225+
private function getCategoryIdsFromIndex(array $productIds): array
222226
{
223227
$categoryIds = [];
224228
foreach ($this->storeManager->getStores() as $store) {
225-
$categoryIds = array_merge(
226-
$categoryIds,
227-
$this->connection->fetchCol(
228-
$this->connection->select()
229-
->from($this->getIndexTable($store->getId()), ['category_id'])
230-
->where('product_id IN (?)', $productIds)
231-
->distinct()
232-
)
229+
$storeCategories = $this->connection->fetchCol(
230+
$this->connection->select()
231+
->from($this->getIndexTable($store->getId()), ['category_id'])
232+
->where('product_id IN (?)', $productIds)
233+
->distinct()
233234
);
235+
$categoryIds[] = $storeCategories;
234236
}
235-
$parentCategories = $categoryIds;
237+
$categoryIds = array_merge(...$categoryIds);
238+
239+
$parentCategories = [$categoryIds];
236240
foreach ($categoryIds as $categoryId) {
237241
$parentIds = explode('/', $this->getPathFromCategoryId($categoryId));
238-
$parentCategories = array_merge($parentCategories, $parentIds);
242+
$parentCategories[] = $parentIds;
239243
}
240-
$categoryIds = array_unique($parentCategories);
244+
$categoryIds = array_unique(array_merge(...$parentCategories));
241245

242246
return $categoryIds;
243247
}

app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Magento\Framework\Event\Observer;
2424
use Magento\Framework\Event\ObserverInterface;
2525
use Magento\Framework\Exception\LocalizedException;
26-
use Magento\Framework\Exception\NoSuchEntityException;
2726
use Magento\ImportExport\Model\Import as ImportExport;
2827
use Magento\Store\Model\Store;
2928
use Magento\Store\Model\StoreManagerInterface;
@@ -252,7 +251,7 @@ public function execute(Observer $observer)
252251
* @throws LocalizedException
253252
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
254253
*/
255-
protected function _populateForUrlGeneration($rowData)
254+
private function _populateForUrlGeneration($rowData)
256255
{
257256
$newSku = $this->import->getNewSku($rowData[ImportProduct::COL_SKU]);
258257
$oldSku = $this->import->getOldSku();
@@ -321,7 +320,7 @@ private function isNeedToPopulateForUrlGeneration($rowData, $newSku, $oldSku): b
321320
* @param array $rowData
322321
* @return void
323322
*/
324-
protected function setStoreToProduct(Product $product, array $rowData)
323+
private function setStoreToProduct(Product $product, array $rowData)
325324
{
326325
if (!empty($rowData[ImportProduct::COL_STORE])
327326
&& ($storeId = $this->import->getStoreIdByCode($rowData[ImportProduct::COL_STORE]))
@@ -339,7 +338,7 @@ protected function setStoreToProduct(Product $product, array $rowData)
339338
* @param string $storeId
340339
* @return $this
341340
*/
342-
protected function addProductToImport($product, $storeId)
341+
private function addProductToImport($product, $storeId)
343342
{
344343
if ($product->getVisibility() == (string)Visibility::getOptionArray()[Visibility::VISIBILITY_NOT_VISIBLE]) {
345344
return $this;
@@ -357,7 +356,7 @@ protected function addProductToImport($product, $storeId)
357356
* @param Product $product
358357
* @return $this
359358
*/
360-
protected function populateGlobalProduct($product)
359+
private function populateGlobalProduct($product)
361360
{
362361
foreach ($this->import->getProductWebsites($product->getSku()) as $websiteId) {
363362
foreach ($this->websitesToStoreIds[$websiteId] as $storeId) {
@@ -376,7 +375,7 @@ protected function populateGlobalProduct($product)
376375
* @return UrlRewrite[]
377376
* @throws LocalizedException
378377
*/
379-
protected function generateUrls()
378+
private function generateUrls()
380379
{
381380
$mergeDataProvider = clone $this->mergeDataProviderPrototype;
382381
$mergeDataProvider->merge($this->canonicalUrlRewriteGenerate());
@@ -398,7 +397,7 @@ protected function generateUrls()
398397
* @param int|null $storeId
399398
* @return bool
400399
*/
401-
protected function isGlobalScope($storeId)
400+
private function isGlobalScope($storeId)
402401
{
403402
return null === $storeId || $storeId == Store::DEFAULT_STORE_ID;
404403
}
@@ -408,7 +407,7 @@ protected function isGlobalScope($storeId)
408407
*
409408
* @return UrlRewrite[]
410409
*/
411-
protected function canonicalUrlRewriteGenerate()
410+
private function canonicalUrlRewriteGenerate()
412411
{
413412
$urls = [];
414413
foreach ($this->products as $productId => $productsByStores) {
@@ -433,7 +432,7 @@ protected function canonicalUrlRewriteGenerate()
433432
* @return UrlRewrite[]
434433
* @throws LocalizedException
435434
*/
436-
protected function categoriesUrlRewriteGenerate()
435+
private function categoriesUrlRewriteGenerate(): array
437436
{
438437
$urls = [];
439438
foreach ($this->products as $productId => $productsByStores) {
@@ -444,25 +443,32 @@ protected function categoriesUrlRewriteGenerate()
444443
continue;
445444
}
446445
$requestPath = $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category);
447-
$urls[] = $this->urlRewriteFactory->create()
448-
->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)
449-
->setEntityId($productId)
450-
->setRequestPath($requestPath)
451-
->setTargetPath($this->productUrlPathGenerator->getCanonicalUrlPath($product, $category))
452-
->setStoreId($storeId)
453-
->setMetadata(['category_id' => $category->getId()]);
446+
$urls[] = [
447+
$this->urlRewriteFactory->create()
448+
->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)
449+
->setEntityId($productId)
450+
->setRequestPath($requestPath)
451+
->setTargetPath($this->productUrlPathGenerator->getCanonicalUrlPath($product, $category))
452+
->setStoreId($storeId)
453+
->setMetadata(['category_id' => $category->getId()])
454+
];
455+
$parentCategoryIds = $category->getAnchorsAbove();
456+
if ($parentCategoryIds) {
457+
$urls[] = $this->getParentCategoriesUrlRewrites($parentCategoryIds, $storeId, $product);
458+
}
454459
}
455460
}
456461
}
457-
return $urls;
462+
$result = !empty($urls) ? array_merge(...$urls) : [];
463+
return $result;
458464
}
459465

460466
/**
461467
* Generate list based on current rewrites
462468
*
463469
* @return UrlRewrite[]
464470
*/
465-
protected function currentUrlRewritesRegenerate()
471+
private function currentUrlRewritesRegenerate()
466472
{
467473
$currentUrlRewrites = $this->urlFinder->findAllByData(
468474
[
@@ -496,7 +502,7 @@ protected function currentUrlRewritesRegenerate()
496502
* @param Category $category
497503
* @return array
498504
*/
499-
protected function generateForAutogenerated($url, $category)
505+
private function generateForAutogenerated($url, $category)
500506
{
501507
$storeId = $url->getStoreId();
502508
$productId = $url->getEntityId();
@@ -532,7 +538,7 @@ protected function generateForAutogenerated($url, $category)
532538
* @param Category $category
533539
* @return array
534540
*/
535-
protected function generateForCustom($url, $category)
541+
private function generateForCustom($url, $category)
536542
{
537543
$storeId = $url->getStoreId();
538544
$productId = $url->getEntityId();
@@ -566,7 +572,7 @@ protected function generateForCustom($url, $category)
566572
* @param UrlRewrite $url
567573
* @return Category|null|bool
568574
*/
569-
protected function retrieveCategoryFromMetadata($url)
575+
private function retrieveCategoryFromMetadata($url)
570576
{
571577
$metadata = $url->getMetadata();
572578
if (isset($metadata['category_id'])) {
@@ -576,32 +582,6 @@ protected function retrieveCategoryFromMetadata($url)
576582
return null;
577583
}
578584

579-
/**
580-
* Check, category suited for url-rewrite generation.
581-
*
582-
* @param Category $category
583-
* @param int $storeId
584-
* @return bool
585-
* @throws NoSuchEntityException
586-
*/
587-
protected function isCategoryProperForGenerating($category, $storeId)
588-
{
589-
if (isset($this->acceptableCategories[$storeId]) &&
590-
isset($this->acceptableCategories[$storeId][$category->getId()])) {
591-
return $this->acceptableCategories[$storeId][$category->getId()];
592-
}
593-
$acceptable = false;
594-
if ($category->getParentId() != Category::TREE_ROOT_ID) {
595-
list(, $rootCategoryId) = $category->getParentIds();
596-
$acceptable = ($rootCategoryId == $this->storeManager->getStore($storeId)->getRootCategoryId());
597-
}
598-
if (!isset($this->acceptableCategories[$storeId])) {
599-
$this->acceptableCategories[$storeId] = [];
600-
}
601-
$this->acceptableCategories[$storeId][$category->getId()] = $acceptable;
602-
return $acceptable;
603-
}
604-
605585
/**
606586
* Get category by id considering store scope.
607587
*
@@ -635,4 +615,36 @@ private function isCategoryRewritesEnabled()
635615
{
636616
return (bool)$this->scopeConfig->getValue('catalog/seo/generate_category_product_rewrites');
637617
}
618+
619+
/**
620+
* Generate url-rewrite for anchor parent-categories.
621+
*
622+
* @param array $categoryIds
623+
* @param int $storeId
624+
* @param Product $product
625+
* @return array
626+
* @throws LocalizedException
627+
*/
628+
private function getParentCategoriesUrlRewrites(array $categoryIds, int $storeId, Product $product): array
629+
{
630+
$urls = [];
631+
foreach ($categoryIds as $categoryId) {
632+
$category = $this->getCategoryById($categoryId, $storeId);
633+
if ($category->getParentId() == Category::TREE_ROOT_ID) {
634+
continue;
635+
}
636+
$requestPath = $this->productUrlPathGenerator
637+
->getUrlPathWithSuffix($product, $storeId, $category);
638+
$targetPath = $this->productUrlPathGenerator
639+
->getCanonicalUrlPath($product, $category);
640+
$urls[] = $this->urlRewriteFactory->create()
641+
->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)
642+
->setEntityId($product->getId())
643+
->setRequestPath($requestPath)
644+
->setTargetPath($targetPath)
645+
->setStoreId($storeId)
646+
->setMetadata(['category_id' => $category->getId()]);
647+
}
648+
return $urls;
649+
}
638650
}

0 commit comments

Comments
 (0)