Skip to content

Commit ea833f4

Browse files
committed
WZ-4506:Fixed Category URL issue when there is multiple stores.
1 parent 43b16f3 commit ea833f4

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

Services/Catalogue/Mappers/ConfigurableProductsData.php

+42-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Wizzy\Search\Services\Queue\SessionStorage\CategoriesSessionStorage;
1414
use Wizzy\Search\Services\Store\StoreAutocompleteConfig;
1515
use Magento\Framework\Event\ManagerInterface;
16+
use Wizzy\Search\Services\Store\StoreManager;
1617

1718
class ConfigurableProductsData
1819
{
@@ -22,7 +23,7 @@ class ConfigurableProductsData
2223
private $genderConfigurable;
2324
private $colorConfigurable;
2425
private $sizeConfigurable;
25-
26+
private $storeManager;
2627
private $storeAutocompleteConfig;
2728

2829
private $categoriesManager;
@@ -36,6 +37,10 @@ class ConfigurableProductsData
3637

3738
private $eventManager;
3839

40+
private $storeId;
41+
private $allStoreBaseUrls = null;
42+
private $currentStoreBaseUrl = [];
43+
3944
public function __construct(
4045
ManagerInterface $eventManager,
4146
BrandConfigurable $brandConfigurable,
@@ -46,7 +51,8 @@ public function __construct(
4651
StoreAutocompleteConfig $storeAutocompleteConfig,
4752
AttributesManager $attributesManager,
4853
CategoriesSessionStorage $categoriesSessionStorage,
49-
ProductsAttributesManager $productsAttributesManager
54+
ProductsAttributesManager $productsAttributesManager,
55+
StoreManager $storeManager
5056
) {
5157
$this->eventManager = $eventManager;
5258
$this->brandConfigurable = $brandConfigurable;
@@ -62,8 +68,14 @@ public function __construct(
6268
$this->productsAttributesManager = $productsAttributesManager;
6369
$this->hasToIgnoreCategories = $this->storeAutocompleteConfig->hasToIgnoreCategories();
6470
$this->categoriesToIgnoreInAutoComplete = $this->storeAutocompleteConfig->getIgnoredCategories();
71+
$this->storeManager = $storeManager;
6572
}
6673

74+
public function setStore($storeId)
75+
{
76+
$this->storeId = $storeId;
77+
}
78+
6779
public function getBrand($categories, $attributes, $storeId)
6880
{
6981
return $this->brandConfigurable->getValue($categories, $attributes, $storeId);
@@ -233,7 +245,6 @@ public function getProductCategories($product)
233245
);
234246
return $dataObject->getDataByKey('categories');
235247
}
236-
237248
private function getCategoryArray($category)
238249
{
239250
$pathIds = $category->getPathIds();
@@ -266,6 +277,32 @@ private function getCategoryArray($category)
266277
$parentUrlKey = '';
267278
}
268279

280+
if ($this->allStoreBaseUrls === null) {
281+
$this->allStoreBaseUrls = $this->storeManager->getAllStoreBaseUrls();
282+
}
283+
284+
if (!isset($this->currentStoreBaseUrl[$this->storeId])) {
285+
$this->currentStoreBaseUrl[$this->storeId] = $this->storeManager->getCurrentStoreBaseUrl();
286+
;
287+
}
288+
289+
if ($this->currentStoreBaseUrl[$this->storeId] &&
290+
isset($this->currentStoreBaseUrl[$this->storeId]['base_url'])
291+
) {
292+
$currentStoreBaseUrl = $this->currentStoreBaseUrl[$this->storeId];
293+
$currentStoreBaseUrl = $currentStoreBaseUrl['base_url'];
294+
}
295+
296+
$categoryUrl = $category->getUrl();
297+
foreach ($this->allStoreBaseUrls as $store) {
298+
if (strpos($categoryUrl, $store['base_url']) === 0) {
299+
$categoryUrl = str_replace($store['base_url'], '', $categoryUrl);
300+
break;
301+
}
302+
}
303+
304+
$categoryUrl = $currentStoreBaseUrl."".$categoryUrl;
305+
269306
$data =
270307
['id' => $category->getId(),
271308
'value' => $category->getName(),
@@ -276,7 +313,7 @@ private function getCategoryArray($category)
276313
'level' => (int) $category->getLevel(),
277314
'description' => ($category->getDescription()) ? $category->getDescription() : '',
278315
'image' => ($category->getImageUrl()) ? $category->getImageUrl() : '',
279-
'url' => $category->getUrl(),
316+
'url' => $categoryUrl,
280317
'isActive'=> $category->getIsActive(),
281318
'pathIds' => $pathIds,
282319
'parentId' => $parentId,
@@ -293,7 +330,7 @@ private function getCategoryArray($category)
293330

294331
$data['includeInMenu'] = $includeInMenu;
295332
$data['isSearchable'] = $isSearchable;
296-
333+
297334
return $data;
298335
}
299336
}

Services/Catalogue/Mappers/ProductsMapper.php

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public function mapAll($products, $productReviews, $orderItems, $storeId)
112112
$this->storeCatalogueConfig->setStore($storeId);
113113
$this->productURLManager->setStore($storeId);
114114
$this->productURLManager->fetchUrls($products);
115+
$this->configurableProductsData->setStore($storeId);
115116
$this->setAdminUrl();
116117
$this->isBrandMandatory = $this->storeCatalogueConfig->isBrandMandatoryForSync();
117118
$this->resetEntitiesToIgnore();

Services/Store/StoreManager.php

+27
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,31 @@ public function getAllStores()
128128

129129
return $storeIds;
130130
}
131+
132+
public function getAllStoreBaseUrls()
133+
{
134+
$baseUrls = [];
135+
$storeIds = $this->getActivateWizzyStores();
136+
137+
foreach ($storeIds as $storeId) {
138+
$store = $this->storeManager->getStore($storeId);
139+
$storeId = $store->getId();
140+
$baseUrl = $store->getBaseUrl();
141+
142+
$baseUrls[] = [
143+
'store_id' => $storeId,
144+
'base_url' => $baseUrl,
145+
];
146+
}
147+
148+
return $baseUrls;
149+
}
150+
151+
public function getCurrentStoreBaseUrl()
152+
{
153+
return $currentStore = [
154+
'store_id' => $this->storeManager->getStore()->getId(),
155+
'base_url' => $this->storeManager->getStore()->getBaseUrl(),
156+
];
157+
}
131158
}

0 commit comments

Comments
 (0)