Skip to content

Commit 8eb2ed2

Browse files
committed
MAGETWO-47656: [GITHUB] Categories Path for Product URLs not working with anchor #2619
- add AnchorUrlRewriteGenerator
1 parent 95abd47 commit 8eb2ed2

File tree

4 files changed

+135
-3
lines changed

4 files changed

+135
-3
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogUrlRewrite\Model\Product;
7+
8+
use Magento\Catalog\Api\CategoryRepositoryInterface;
9+
use Magento\Catalog\Model\Product;
10+
use Magento\CatalogUrlRewrite\Model\ObjectRegistry;
11+
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
12+
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
15+
use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory;
16+
17+
class AnchorUrlRewriteGenerator
18+
{
19+
/** @var ProductUrlPathGenerator */
20+
protected $urlPathGenerator;
21+
22+
/** @var UrlRewriteFactory */
23+
protected $urlRewriteFactory;
24+
25+
/** @var CategoryRepositoryInterface */
26+
private $categoryRepository;
27+
28+
/**
29+
* @param ProductUrlPathGenerator $urlPathGenerator
30+
* @param UrlRewriteFactory $urlRewriteFactory
31+
* @param CategoryRepositoryInterface $categoryRepository
32+
*/
33+
public function __construct(
34+
ProductUrlPathGenerator $urlPathGenerator,
35+
UrlRewriteFactory $urlRewriteFactory,
36+
CategoryRepositoryInterface $categoryRepository
37+
) {
38+
$this->urlPathGenerator = $urlPathGenerator;
39+
$this->urlRewriteFactory = $urlRewriteFactory;
40+
$this->categoryRepository = $categoryRepository;
41+
}
42+
43+
/**
44+
* Generate list based on categories
45+
*
46+
* @param int $storeId
47+
* @param Product $product
48+
* @param ObjectRegistry $productCategories
49+
* @return UrlRewrite[]
50+
*/
51+
public function generate($storeId, Product $product, ObjectRegistry $productCategories)
52+
{
53+
$urls = [];
54+
foreach ($productCategories->getList() as $category) {
55+
$anchorCategoryIds = $category->getAnchorsAbove();
56+
if ($anchorCategoryIds) {
57+
foreach ($anchorCategoryIds as $anchorCategoryId) {
58+
try {
59+
$anchorCategory = $this->categoryRepository->get($anchorCategoryId);
60+
$urls[] = $this->urlRewriteFactory->create()
61+
->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)
62+
->setEntityId($product->getId())
63+
->setRequestPath(
64+
$this->urlPathGenerator->getUrlPathWithSuffix(
65+
$product,
66+
$storeId,
67+
$anchorCategory
68+
)
69+
)
70+
->setTargetPath(
71+
$this->urlPathGenerator->getCanonicalUrlPath(
72+
$product,
73+
$anchorCategory
74+
)
75+
)
76+
->setStoreId($storeId)
77+
->setMetadata(['category_id' => $anchorCategory->getId()]);
78+
} catch (NoSuchEntityException $e) {
79+
continue;
80+
}
81+
82+
}
83+
}
84+
}
85+
86+
return $urls;
87+
}
88+
}

app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\CatalogUrlRewrite\Model\Product\CanonicalUrlRewriteGenerator;
1010
use Magento\CatalogUrlRewrite\Model\Product\CategoriesUrlRewriteGenerator;
1111
use Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator;
12+
use Magento\CatalogUrlRewrite\Model\Product\AnchorUrlRewriteGenerator;
1213
use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
1314
use Magento\Store\Model\Store;
1415

@@ -43,6 +44,9 @@ class ProductUrlRewriteGenerator
4344
/** @var \Magento\Store\Model\StoreManagerInterface */
4445
protected $storeManager;
4546

47+
/** @var AnchorUrlRewriteGenerator */
48+
private $anchorUrlRewriteGenerator;
49+
4650
/**
4751
* @param \Magento\CatalogUrlRewrite\Model\Product\CanonicalUrlRewriteGenerator $canonicalUrlRewriteGenerator
4852
* @param \Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator $currentUrlRewritesRegenerator
@@ -67,6 +71,20 @@ public function __construct(
6771
$this->storeManager = $storeManager;
6872
}
6973

74+
/**
75+
* @return AnchorUrlRewriteGenerator
76+
*
77+
* @deprecated
78+
*/
79+
private function getAnchorUrlRewriteGenerator()
80+
{
81+
if ($this->anchorUrlRewriteGenerator === null) {
82+
$this->anchorUrlRewriteGenerator = \Magento\Framework\App\ObjectManager::getInstance()
83+
->get('Magento\CatalogUrlRewrite\Model\Product\AnchorUrlRewriteGenerator');
84+
}
85+
return $this->anchorUrlRewriteGenerator;
86+
}
87+
7088
/**
7189
* Generate product url rewrites
7290
*
@@ -143,7 +161,8 @@ protected function generateForSpecificStoreView($storeId, $productCategories)
143161
$urls = array_merge(
144162
$this->canonicalUrlRewriteGenerator->generate($storeId, $this->product),
145163
$this->categoriesUrlRewriteGenerator->generate($storeId, $this->product, $this->productCategories),
146-
$this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->productCategories)
164+
$this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->productCategories),
165+
$this->getAnchorUrlRewriteGenerator()->generate($storeId, $this->product, $this->productCategories)
147166
);
148167

149168
/* Reduce duplicates. Last wins */

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ public function execute(\Magento\Framework\Event\Observer $observer)
4949
if ($category->getParentId() == Category::TREE_ROOT_ID) {
5050
return;
5151
}
52-
if ($category->dataHasChangedFor('url_key') || $category->getIsChangedProductList()) {
52+
if ($category->dataHasChangedFor('url_key')
53+
|| $category->dataHasChangedFor('is_anchor')
54+
|| $category->getIsChangedProductList()
55+
) {
5356
$urlRewrites = array_merge(
5457
$this->categoryUrlRewriteGenerator->generate($category),
5558
$this->urlRewriteHandler->generateProductUrlRewrites($category)

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductUrlRewriteGeneratorTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class ProductUrlRewriteGeneratorTest extends \PHPUnit_Framework_TestCase
2222
/** @var \PHPUnit_Framework_MockObject_MockObject */
2323
protected $categoriesUrlRewriteGenerator;
2424

25+
/** @var \PHPUnit_Framework_MockObject_MockObject */
26+
protected $anchorUrlRewriteGenerator;
27+
2528
/** @var \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator */
2629
protected $productUrlRewriteGenerator;
2730

@@ -63,6 +66,9 @@ protected function setUp()
6366
$this->categoriesUrlRewriteGenerator = $this->getMockBuilder(
6467
'Magento\CatalogUrlRewrite\Model\Product\CategoriesUrlRewriteGenerator'
6568
)->disableOriginalConstructor()->getMock();
69+
$this->anchorUrlRewriteGenerator = $this->getMockBuilder(
70+
'Magento\CatalogUrlRewrite\Model\Product\AnchorUrlRewriteGenerator'
71+
)->disableOriginalConstructor()->getMock();
6672
$this->objectRegistryFactory = $this->getMockBuilder('Magento\CatalogUrlRewrite\Model\ObjectRegistryFactory')
6773
->disableOriginalConstructor()->setMethods(['create'])->getMock();
6874
$this->storeViewService = $this->getMockBuilder('Magento\CatalogUrlRewrite\Service\V1\StoreViewService')
@@ -108,9 +114,19 @@ public function testGenerationForGlobalScope()
108114
->setStoreId(3);
109115
$this->currentUrlRewritesRegenerator->expects($this->any())->method('generate')
110116
->will($this->returnValue([$current]));
117+
$anchorCategories = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite();
118+
$anchorCategories->setTargetPath('category-4')
119+
->setStoreId(4);
120+
$this->anchorUrlRewriteGenerator->expects($this->any())->method('generate')
121+
->will($this->returnValue([$anchorCategories]));
111122

112123
$this->assertEquals(
113-
['category-1-1' => $canonical, 'category-2-2' => $categories, 'category-3-3' => $current],
124+
[
125+
'category-1-1' => $canonical,
126+
'category-2-2' => $categories,
127+
'category-3-3' => $current,
128+
'category-4-4' => $anchorCategories
129+
],
114130
$this->productUrlRewriteGenerator->generate($this->product)
115131
);
116132
}
@@ -143,6 +159,8 @@ public function testGenerationForSpecificStore()
143159
->will($this->returnValue([]));
144160
$this->currentUrlRewritesRegenerator->expects($this->any())->method('generate')
145161
->will($this->returnValue([]));
162+
$this->anchorUrlRewriteGenerator->expects($this->any())->method('generate')
163+
->will($this->returnValue([]));
146164

147165
$this->assertEquals(['category-1-1' => $canonical], $this->productUrlRewriteGenerator->generate($this->product));
148166
}
@@ -172,6 +190,8 @@ public function testSkipRootCategoryForCategoriesGenerator()
172190
->will($this->returnValue([]));
173191
$this->currentUrlRewritesRegenerator->expects($this->any())->method('generate')
174192
->will($this->returnValue([]));
193+
$this->anchorUrlRewriteGenerator->expects($this->any())->method('generate')
194+
->will($this->returnValue([]));
175195

176196
$this->assertEquals(['category-1-1' => $canonical], $this->productUrlRewriteGenerator->generate($this->product));
177197
}
@@ -201,6 +221,8 @@ public function testSkipGenerationForNotStoreRootCategory()
201221
->will($this->returnValue([]));
202222
$this->currentUrlRewritesRegenerator->expects($this->any())->method('generate')
203223
->will($this->returnValue([]));
224+
$this->anchorUrlRewriteGenerator->expects($this->any())->method('generate')
225+
->will($this->returnValue([]));
204226

205227
$this->assertEquals(['category-1-1' => $canonical], $this->productUrlRewriteGenerator->generate($this->product));
206228
}

0 commit comments

Comments
 (0)