23
23
use Magento \Framework \Event \Observer ;
24
24
use Magento \Framework \Event \ObserverInterface ;
25
25
use Magento \Framework \Exception \LocalizedException ;
26
- use Magento \Framework \Exception \NoSuchEntityException ;
27
26
use Magento \ImportExport \Model \Import as ImportExport ;
28
27
use Magento \Store \Model \Store ;
29
28
use Magento \Store \Model \StoreManagerInterface ;
@@ -252,7 +251,7 @@ public function execute(Observer $observer)
252
251
* @throws LocalizedException
253
252
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
254
253
*/
255
- protected function _populateForUrlGeneration ($ rowData )
254
+ private function _populateForUrlGeneration ($ rowData )
256
255
{
257
256
$ newSku = $ this ->import ->getNewSku ($ rowData [ImportProduct::COL_SKU ]);
258
257
$ oldSku = $ this ->import ->getOldSku ();
@@ -321,7 +320,7 @@ private function isNeedToPopulateForUrlGeneration($rowData, $newSku, $oldSku): b
321
320
* @param array $rowData
322
321
* @return void
323
322
*/
324
- protected function setStoreToProduct (Product $ product , array $ rowData )
323
+ private function setStoreToProduct (Product $ product , array $ rowData )
325
324
{
326
325
if (!empty ($ rowData [ImportProduct::COL_STORE ])
327
326
&& ($ storeId = $ this ->import ->getStoreIdByCode ($ rowData [ImportProduct::COL_STORE ]))
@@ -339,7 +338,7 @@ protected function setStoreToProduct(Product $product, array $rowData)
339
338
* @param string $storeId
340
339
* @return $this
341
340
*/
342
- protected function addProductToImport ($ product , $ storeId )
341
+ private function addProductToImport ($ product , $ storeId )
343
342
{
344
343
if ($ product ->getVisibility () == (string )Visibility::getOptionArray ()[Visibility::VISIBILITY_NOT_VISIBLE ]) {
345
344
return $ this ;
@@ -357,7 +356,7 @@ protected function addProductToImport($product, $storeId)
357
356
* @param Product $product
358
357
* @return $this
359
358
*/
360
- protected function populateGlobalProduct ($ product )
359
+ private function populateGlobalProduct ($ product )
361
360
{
362
361
foreach ($ this ->import ->getProductWebsites ($ product ->getSku ()) as $ websiteId ) {
363
362
foreach ($ this ->websitesToStoreIds [$ websiteId ] as $ storeId ) {
@@ -376,7 +375,7 @@ protected function populateGlobalProduct($product)
376
375
* @return UrlRewrite[]
377
376
* @throws LocalizedException
378
377
*/
379
- protected function generateUrls ()
378
+ private function generateUrls ()
380
379
{
381
380
$ mergeDataProvider = clone $ this ->mergeDataProviderPrototype ;
382
381
$ mergeDataProvider ->merge ($ this ->canonicalUrlRewriteGenerate ());
@@ -398,7 +397,7 @@ protected function generateUrls()
398
397
* @param int|null $storeId
399
398
* @return bool
400
399
*/
401
- protected function isGlobalScope ($ storeId )
400
+ private function isGlobalScope ($ storeId )
402
401
{
403
402
return null === $ storeId || $ storeId == Store::DEFAULT_STORE_ID ;
404
403
}
@@ -408,7 +407,7 @@ protected function isGlobalScope($storeId)
408
407
*
409
408
* @return UrlRewrite[]
410
409
*/
411
- protected function canonicalUrlRewriteGenerate ()
410
+ private function canonicalUrlRewriteGenerate ()
412
411
{
413
412
$ urls = [];
414
413
foreach ($ this ->products as $ productId => $ productsByStores ) {
@@ -433,7 +432,7 @@ protected function canonicalUrlRewriteGenerate()
433
432
* @return UrlRewrite[]
434
433
* @throws LocalizedException
435
434
*/
436
- protected function categoriesUrlRewriteGenerate ()
435
+ private function categoriesUrlRewriteGenerate (): array
437
436
{
438
437
$ urls = [];
439
438
foreach ($ this ->products as $ productId => $ productsByStores ) {
@@ -444,25 +443,32 @@ protected function categoriesUrlRewriteGenerate()
444
443
continue ;
445
444
}
446
445
$ 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
+ }
454
459
}
455
460
}
456
461
}
457
- return $ urls ;
462
+ $ result = !empty ($ urls ) ? array_merge (...$ urls ) : [];
463
+ return $ result ;
458
464
}
459
465
460
466
/**
461
467
* Generate list based on current rewrites
462
468
*
463
469
* @return UrlRewrite[]
464
470
*/
465
- protected function currentUrlRewritesRegenerate ()
471
+ private function currentUrlRewritesRegenerate ()
466
472
{
467
473
$ currentUrlRewrites = $ this ->urlFinder ->findAllByData (
468
474
[
@@ -496,7 +502,7 @@ protected function currentUrlRewritesRegenerate()
496
502
* @param Category $category
497
503
* @return array
498
504
*/
499
- protected function generateForAutogenerated ($ url , $ category )
505
+ private function generateForAutogenerated ($ url , $ category )
500
506
{
501
507
$ storeId = $ url ->getStoreId ();
502
508
$ productId = $ url ->getEntityId ();
@@ -532,7 +538,7 @@ protected function generateForAutogenerated($url, $category)
532
538
* @param Category $category
533
539
* @return array
534
540
*/
535
- protected function generateForCustom ($ url , $ category )
541
+ private function generateForCustom ($ url , $ category )
536
542
{
537
543
$ storeId = $ url ->getStoreId ();
538
544
$ productId = $ url ->getEntityId ();
@@ -566,7 +572,7 @@ protected function generateForCustom($url, $category)
566
572
* @param UrlRewrite $url
567
573
* @return Category|null|bool
568
574
*/
569
- protected function retrieveCategoryFromMetadata ($ url )
575
+ private function retrieveCategoryFromMetadata ($ url )
570
576
{
571
577
$ metadata = $ url ->getMetadata ();
572
578
if (isset ($ metadata ['category_id ' ])) {
@@ -576,32 +582,6 @@ protected function retrieveCategoryFromMetadata($url)
576
582
return null ;
577
583
}
578
584
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
-
605
585
/**
606
586
* Get category by id considering store scope.
607
587
*
@@ -635,4 +615,36 @@ private function isCategoryRewritesEnabled()
635
615
{
636
616
return (bool )$ this ->scopeConfig ->getValue ('catalog/seo/generate_category_product_rewrites ' );
637
617
}
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
+ }
638
650
}
0 commit comments