From b9b95320ce8c13247b0fc7ba2c259cf90636eff7 Mon Sep 17 00:00:00 2001 From: smit-kandiya Date: Tue, 22 Oct 2024 19:25:05 +0530 Subject: [PATCH] WZ-4506:Fixed Category URL issue when there is multiple stores. --- .../Mappers/ConfigurableProductsData.php | 48 +++++++++++++++++-- Services/Catalogue/Mappers/ProductsMapper.php | 1 + Services/Store/StoreManager.php | 27 +++++++++++ 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/Services/Catalogue/Mappers/ConfigurableProductsData.php b/Services/Catalogue/Mappers/ConfigurableProductsData.php index fdc202a..03bb878 100644 --- a/Services/Catalogue/Mappers/ConfigurableProductsData.php +++ b/Services/Catalogue/Mappers/ConfigurableProductsData.php @@ -13,6 +13,7 @@ use Wizzy\Search\Services\Queue\SessionStorage\CategoriesSessionStorage; use Wizzy\Search\Services\Store\StoreAutocompleteConfig; use Magento\Framework\Event\ManagerInterface; +use Wizzy\Search\Services\Store\StoreManager; class ConfigurableProductsData { @@ -22,7 +23,7 @@ class ConfigurableProductsData private $genderConfigurable; private $colorConfigurable; private $sizeConfigurable; - + private $storeManager; private $storeAutocompleteConfig; private $categoriesManager; @@ -36,6 +37,10 @@ class ConfigurableProductsData private $eventManager; + private $storeId; + private $allStoreBaseUrls = null; + private $currentStoreBaseUrl = []; + public function __construct( ManagerInterface $eventManager, BrandConfigurable $brandConfigurable, @@ -46,7 +51,8 @@ public function __construct( StoreAutocompleteConfig $storeAutocompleteConfig, AttributesManager $attributesManager, CategoriesSessionStorage $categoriesSessionStorage, - ProductsAttributesManager $productsAttributesManager + ProductsAttributesManager $productsAttributesManager, + StoreManager $storeManager ) { $this->eventManager = $eventManager; $this->brandConfigurable = $brandConfigurable; @@ -62,8 +68,14 @@ public function __construct( $this->productsAttributesManager = $productsAttributesManager; $this->hasToIgnoreCategories = $this->storeAutocompleteConfig->hasToIgnoreCategories(); $this->categoriesToIgnoreInAutoComplete = $this->storeAutocompleteConfig->getIgnoredCategories(); + $this->storeManager = $storeManager; } + public function setStore($storeId) + { + $this->storeId = $storeId; + } + public function getBrand($categories, $attributes, $storeId) { return $this->brandConfigurable->getValue($categories, $attributes, $storeId); @@ -233,7 +245,6 @@ public function getProductCategories($product) ); return $dataObject->getDataByKey('categories'); } - private function getCategoryArray($category) { $pathIds = $category->getPathIds(); @@ -266,6 +277,33 @@ private function getCategoryArray($category) $parentUrlKey = ''; } + if ($this->allStoreBaseUrls === null) { + $this->allStoreBaseUrls = $this->storeManager->getAllStoreBaseUrls(); + } + + if (!isset($this->currentStoreBaseUrl[$this->storeId])) { + $this->currentStoreBaseUrl[$this->storeId] = $this->storeManager->getCurrentStoreBaseUrl(); + ; + } + + if ( + $this->currentStoreBaseUrl[$this->storeId] && + isset($this->currentStoreBaseUrl[$this->storeId]['base_url']) + ) { + $currentStoreBaseUrl = $this->currentStoreBaseUrl[$this->storeId]; + $currentStoreBaseUrl = $currentStoreBaseUrl['base_url']; + } + + $categoryUrl = $category->getUrl(); + foreach ($this->allStoreBaseUrls as $store) { + if (strpos($categoryUrl, $store['base_url']) === 0) { + $categoryUrl = str_replace($store['base_url'], '', $categoryUrl); + break; + } + } + + $categoryUrl = $currentStoreBaseUrl."".$categoryUrl; + $data = ['id' => $category->getId(), 'value' => $category->getName(), @@ -276,7 +314,7 @@ private function getCategoryArray($category) 'level' => (int) $category->getLevel(), 'description' => ($category->getDescription()) ? $category->getDescription() : '', 'image' => ($category->getImageUrl()) ? $category->getImageUrl() : '', - 'url' => $category->getUrl(), + 'url' => $categoryUrl, 'isActive'=> $category->getIsActive(), 'pathIds' => $pathIds, 'parentId' => $parentId, @@ -293,7 +331,7 @@ private function getCategoryArray($category) $data['includeInMenu'] = $includeInMenu; $data['isSearchable'] = $isSearchable; - + return $data; } } diff --git a/Services/Catalogue/Mappers/ProductsMapper.php b/Services/Catalogue/Mappers/ProductsMapper.php index 4fc7346..81b8c19 100755 --- a/Services/Catalogue/Mappers/ProductsMapper.php +++ b/Services/Catalogue/Mappers/ProductsMapper.php @@ -112,6 +112,7 @@ public function mapAll($products, $productReviews, $orderItems, $storeId) $this->storeCatalogueConfig->setStore($storeId); $this->productURLManager->setStore($storeId); $this->productURLManager->fetchUrls($products); + $this->configurableProductsData->setStore($storeId); $this->setAdminUrl(); $this->isBrandMandatory = $this->storeCatalogueConfig->isBrandMandatoryForSync(); $this->resetEntitiesToIgnore(); diff --git a/Services/Store/StoreManager.php b/Services/Store/StoreManager.php index e95910b..b2c7c96 100644 --- a/Services/Store/StoreManager.php +++ b/Services/Store/StoreManager.php @@ -128,4 +128,31 @@ public function getAllStores() return $storeIds; } + + public function getAllStoreBaseUrls() + { + $baseUrls = []; + $storeIds = $this->getActivateWizzyStores(); + + foreach ($storeIds as $storeId) { + $store = $this->storeManager->getStore($storeId); + $storeId = $store->getId(); + $baseUrl = $store->getBaseUrl(); + + $baseUrls[] = [ + 'store_id' => $storeId, + 'base_url' => $baseUrl, + ]; + } + + return $baseUrls; + } + + public function getCurrentStoreBaseUrl() + { + return $currentStore = [ + 'store_id' => $this->storeManager->getStore()->getId(), + 'base_url' => $this->storeManager->getStore()->getBaseUrl(), + ]; + } }