77
88use Magento \Catalog \Api \CategoryRepositoryInterface ;
99use Magento \Catalog \Api \Data \CategoryInterface ;
10+ use Magento \Catalog \Api \ProductRepositoryInterface ;
1011use Magento \Catalog \Model \Category ;
1112use Magento \Catalog \Model \Product ;
1213use Magento \CatalogUrlRewrite \Model \Product \AnchorUrlRewriteGenerator ;
@@ -78,6 +79,11 @@ class ProductScopeRewriteGenerator
7879 */
7980 private $ categoryRepository ;
8081
82+ /**
83+ * @var ProductRepositoryInterface
84+ */
85+ private $ productRepository ;
86+
8187 /**
8288 * @param StoreViewService $storeViewService
8389 * @param StoreManagerInterface $storeManager
@@ -89,6 +95,7 @@ class ProductScopeRewriteGenerator
8995 * @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory
9096 * @param CategoryRepositoryInterface|null $categoryRepository
9197 * @param ScopeConfigInterface|null $config
98+ * @param ProductRepositoryInterface|null $productRepository
9299 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
93100 */
94101 public function __construct (
@@ -101,7 +108,8 @@ public function __construct(
101108 AnchorUrlRewriteGenerator $ anchorUrlRewriteGenerator ,
102109 MergeDataProviderFactory $ mergeDataProviderFactory = null ,
103110 CategoryRepositoryInterface $ categoryRepository = null ,
104- ScopeConfigInterface $ config = null
111+ ScopeConfigInterface $ config = null ,
112+ ProductRepositoryInterface $ productRepository = null
105113 ) {
106114 $ this ->storeViewService = $ storeViewService ;
107115 $ this ->storeManager = $ storeManager ;
@@ -117,6 +125,8 @@ public function __construct(
117125 $ this ->categoryRepository = $ categoryRepository ?:
118126 ObjectManager::getInstance ()->get (CategoryRepositoryInterface::class);
119127 $ this ->config = $ config ?: ObjectManager::getInstance ()->get (ScopeConfigInterface::class);
128+ $ this ->productRepository = $ productRepository ?:
129+ ObjectManager::getInstance ()->get (ProductRepositoryInterface::class);
120130 }
121131
122132 /**
@@ -144,15 +154,21 @@ public function generateForGlobalScope($productCategories, Product $product, $ro
144154 $ mergeDataProvider = clone $ this ->mergeDataProviderPrototype ;
145155
146156 foreach ($ product ->getStoreIds () as $ id ) {
147- if (!$ this ->isGlobalScope ($ id ) &&
148- !$ this ->storeViewService ->doesEntityHaveOverriddenUrlKeyForStore (
157+ if (!$ this ->isGlobalScope ($ id )) {
158+ if ( !$ this ->storeViewService ->doesEntityHaveOverriddenUrlKeyForStore (
149159 $ id ,
150160 $ productId ,
151161 Product::ENTITY
152162 )) {
153- $ mergeDataProvider ->merge (
154- $ this ->generateForSpecificStoreView ($ id , $ productCategories , $ product , $ rootCategoryId )
155- );
163+ $ mergeDataProvider ->merge (
164+ $ this ->generateForSpecificStoreView ($ id , $ productCategories , $ product , $ rootCategoryId )
165+ );
166+ } else {
167+ $ scopedProduct = $ this ->productRepository ->getById ($ productId , false , $ id );
168+ $ mergeDataProvider ->merge (
169+ $ this ->generateForSpecificStoreView ($ id , $ productCategories , $ scopedProduct , $ rootCategoryId )
170+ );
171+ }
156172 }
157173 }
158174
0 commit comments