6
6
namespace Magento \CatalogUrlRewrite \Model \Product ;
7
7
8
8
use Magento \Catalog \Model \Category ;
9
+ use Magento \Catalog \Model \CategoryRepository ;
9
10
use Magento \Catalog \Model \Product ;
10
11
use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
11
12
use Magento \UrlRewrite \Model \OptionProvider ;
@@ -56,19 +57,24 @@ class CurrentUrlRewritesRegenerator
56
57
/** @var \Magento\UrlRewrite\Model\MergeDataProvider */
57
58
private $ mergeDataProviderPrototype ;
58
59
60
+ /** @var CategoryRepository */
61
+ private $ categoryRepository ;
62
+
59
63
/**
60
64
* @param UrlFinderInterface $urlFinder
61
65
* @param ProductUrlPathGenerator $productUrlPathGenerator
62
66
* @param UrlRewriteFactory $urlRewriteFactory
63
67
* @param UrlRewriteFinder|null $urlRewriteFinder
64
68
* @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory
69
+ * @param CategoryRepository|null $categoryRepository
65
70
*/
66
71
public function __construct (
67
72
UrlFinderInterface $ urlFinder ,
68
73
ProductUrlPathGenerator $ productUrlPathGenerator ,
69
74
UrlRewriteFactory $ urlRewriteFactory ,
70
75
UrlRewriteFinder $ urlRewriteFinder = null ,
71
- MergeDataProviderFactory $ mergeDataProviderFactory = null
76
+ MergeDataProviderFactory $ mergeDataProviderFactory = null ,
77
+ CategoryRepository $ categoryRepository = null
72
78
) {
73
79
$ this ->urlFinder = $ urlFinder ;
74
80
$ this ->productUrlPathGenerator = $ productUrlPathGenerator ;
@@ -78,11 +84,12 @@ public function __construct(
78
84
if (!isset ($ mergeDataProviderFactory )) {
79
85
$ mergeDataProviderFactory = ObjectManager::getInstance ()->get (MergeDataProviderFactory::class);
80
86
}
87
+ $ this ->categoryRepository = $ categoryRepository ?: ObjectManager::getInstance ()->get (CategoryRepository::class);
81
88
$ this ->mergeDataProviderPrototype = $ mergeDataProviderFactory ->create ();
82
89
}
83
90
84
91
/**
85
- * Generate list based on current rewrites
92
+ * Generate product rewrites based on current rewrites without anchor categories
86
93
*
87
94
* @param int $storeId
88
95
* @param Product $product
@@ -115,6 +122,48 @@ public function generate($storeId, Product $product, ObjectRegistry $productCate
115
122
return $ mergeDataProvider ->getData ();
116
123
}
117
124
125
+ /**
126
+ * Generate product rewrites for anchor categories based on current rewrites
127
+ *
128
+ * @param int $storeId
129
+ * @param Product $product
130
+ * @param ObjectRegistry $productCategories
131
+ * @param int|null $rootCategoryId
132
+ * @return UrlRewrite[]
133
+ */
134
+ public function generateAnchor ($ storeId , Product $ product , ObjectRegistry $ productCategories , $ rootCategoryId = null )
135
+ {
136
+ $ anchorCategoryIds = [];
137
+ $ mergeDataProvider = clone $ this ->mergeDataProviderPrototype ;
138
+
139
+ $ currentUrlRewrites = $ this ->urlRewriteFinder ->findAllByData (
140
+ $ product ->getEntityId (),
141
+ $ storeId ,
142
+ ProductUrlRewriteGenerator::ENTITY_TYPE ,
143
+ $ rootCategoryId
144
+ );
145
+
146
+ foreach ($ productCategories ->getList () as $ productCategory ) {
147
+ $ anchorCategoryIds = array_merge ($ productCategory ->getAnchorsAbove (), $ anchorCategoryIds );
148
+ }
149
+
150
+ foreach ($ currentUrlRewrites as $ currentUrlRewrite ) {
151
+ $ metadata = $ currentUrlRewrite ->getMetadata ();
152
+ if (isset ($ metadata ['category_id ' ]) && $ metadata ['category_id ' ] > 0 ) {
153
+ $ category = $ this ->categoryRepository ->get ($ metadata ['category_id ' ], $ storeId );
154
+ if (in_array ($ category ->getId (), $ anchorCategoryIds )) {
155
+ $ mergeDataProvider ->merge (
156
+ $ currentUrlRewrite ->getIsAutogenerated ()
157
+ ? $ this ->generateForAutogenerated ($ currentUrlRewrite , $ storeId , $ category , $ product )
158
+ : $ this ->generateForCustom ($ currentUrlRewrite , $ storeId , $ category , $ product )
159
+ );
160
+ }
161
+ }
162
+ }
163
+
164
+ return $ mergeDataProvider ->getData ();
165
+ }
166
+
118
167
/**
119
168
* @param UrlRewrite $url
120
169
* @param int $storeId
0 commit comments