Skip to content

Commit

Permalink
WZ-4506:Fixed Category URL issue when there is multiple stores.
Browse files Browse the repository at this point in the history
  • Loading branch information
smit-kandiya committed Oct 22, 2024
1 parent 43b16f3 commit faac839
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
45 changes: 40 additions & 5 deletions Services/Catalogue/Mappers/ConfigurableProductsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -22,7 +23,7 @@ class ConfigurableProductsData
private $genderConfigurable;
private $colorConfigurable;
private $sizeConfigurable;

private $storeManager;
private $storeAutocompleteConfig;

private $categoriesManager;
Expand All @@ -36,6 +37,10 @@ class ConfigurableProductsData

private $eventManager;

private $storeId;
private $allStoreBaseUrls = null;
private $currentStoreBaseUrl = [];

public function __construct(
ManagerInterface $eventManager,
BrandConfigurable $brandConfigurable,
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -233,7 +245,6 @@ public function getProductCategories($product)
);
return $dataObject->getDataByKey('categories');
}

private function getCategoryArray($category)
{
$pathIds = $category->getPathIds();
Expand Down Expand Up @@ -266,6 +277,30 @@ private function getCategoryArray($category)
$parentUrlKey = '';
}

if ($this->allStoreBaseUrls === null) {
$this->allStoreBaseUrls = $this->storeManager->getAllStoreBaseUrls();
}

$storeUrls = $this->allStoreBaseUrls;

if (!isset($this->currentStoreBaseUrl[$this->storeId])) {
$this->currentStoreBaseUrl[$this->storeId] = $this->storeManager->getCurrentStoreBaseUrl();
;
}

$currentStoreBaseUrl = $this->currentStoreBaseUrl[$this->storeId];
$currentStoreBaseUrl = $currentStoreBaseUrl['base_url'];

$categoryUrl = $category->getUrl();
foreach ($storeUrls 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(),
Expand All @@ -276,7 +311,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,
Expand All @@ -293,7 +328,7 @@ private function getCategoryArray($category)

$data['includeInMenu'] = $includeInMenu;
$data['isSearchable'] = $isSearchable;

return $data;
}
}
1 change: 1 addition & 0 deletions Services/Catalogue/Mappers/ProductsMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
27 changes: 27 additions & 0 deletions Services/Store/StoreManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
}
}

0 comments on commit faac839

Please sign in to comment.