Skip to content

Commit

Permalink
Merge pull request #15 from outeredge/store-code-fix
Browse files Browse the repository at this point in the history
Fix store code in alternate URL
  • Loading branch information
vseager authored Jun 29, 2023
2 parents d9a79a2 + 41fc58f commit 9e0c0c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
36 changes: 25 additions & 11 deletions Model/Config/Source/StoreLang.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\App\State as MagentoState;
use Magento\LayeredNavigation\Block\Navigation\State;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
Expand Down Expand Up @@ -44,6 +46,11 @@ class StoreLang
*/
protected $urlFinder;

/**
* @var MagentoState
*/
protected $magentoState;

/**
* @var State
*/
Expand All @@ -69,15 +76,20 @@ class StoreLang
* @param StoreManagerInterface $storeManager
* @param ScopeConfigInterface $scopeConfig
* @param CategoryRepositoryInterface $categoryRepository
* @param CategoryRepositoryInterface $categoryRepository
* @param ProductRepositoryInterface $productRepository
* @param UrlFinderInterface $urlFinder
* @param MagentoState $magentoState
* @param State $layeredNavState
* @param ModuleManager $moduleManager
* @param ObjectManagerInterface $objectManager
*/
public function __construct(
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig,
CategoryRepositoryInterface $categoryRepository,
ProductRepositoryInterface $productRepository,
UrlFinderInterface $urlFinder,
MagentoState $magentoState,
State $layeredNavState,
ModuleManager $moduleManager,
ObjectManagerInterface $objectManager
Expand All @@ -87,11 +99,12 @@ public function __construct(
$this->categoryRepository = $categoryRepository;
$this->productRepository = $productRepository;
$this->urlFinder = $urlFinder;
$this->magentoState = $magentoState;
$this->layeredNavState = $layeredNavState;
$this->moduleManager = $moduleManager;
$this->objectManager = $objectManager;

if ($this->moduleManager->isEnabled('Amasty_ShopbyBase')) {
if ($this->moduleManager->isEnabled('Amasty_ShopbyBase') && $this->magentoState->getAreaCode() == Area::AREA_FRONTEND) {
$this->urlModifier = $this->objectManager->create('Amasty\ShopbyBase\Model\UrlBuilder\UrlModifier');
}
}
Expand Down Expand Up @@ -125,7 +138,7 @@ public function getAllStoresLang($obj = false)
$langPrefix = $this->scopeConfig->getValue('general/locale/code', ScopeInterface::SCOPE_STORE, $store->getStoreId());
}

$langUlr = substr($store->getCurrentUrl(), 0, strpos($store->getCurrentUrl(), "?"));
$langUrl = substr($store->getCurrentUrl(), 0, strpos($store->getCurrentUrl(), "?"));

try {
if ($type == 'category') {
Expand Down Expand Up @@ -159,38 +172,39 @@ public function getAllStoresLang($obj = false)
$queryParams[$filter->getFilter()->getRequestVar()] = $filter->getValue();
}

$langUlr = $store->getUrl($urlPath, ['_query' => $queryParams]);
$this->storeManager->setCurrentStore($store->getStoreId());

$langUrl = $store->getUrl($urlPath, ['_query' => $queryParams]);

if ($stripSlash) {
// Remove slash to avoid redirect
$langUlr = substr_replace($langUlr, '', strrpos($langUlr, '/'), 1);
$langUrl = substr_replace($langUrl, '', strrpos($langUrl, '/'), 1);
}

if ($this->urlModifier) {
// Amasty Shopby
$this->storeManager->setCurrentStore($store->getStoreId());
$langUlr = $this->urlModifier->execute($langUlr, $storeCategory->getId());
$this->storeManager->setCurrentStore($currentStoreId);
$langUrl = strtok($this->urlModifier->execute($langUrl, $storeCategory->getId()), '?');
}
$this->storeManager->setCurrentStore($currentStoreId);
} elseif ($type == 'product') {
$storeProduct = $this->productRepository->getById($obj->getId(), false, $store->getId());

if (!in_array($store->getId(), $obj->getStoreIds())) {
continue;
}

$langUlr = $storeProduct->getProductUrl();
$langUrl = $storeProduct->getProductUrl();
}
} catch (NoSuchEntityException $e) {
continue;
}

if (is_array($langPrefix)) {
foreach($langPrefix as $lang) {
$locale[$lang] = $langUlr;
$locale[$lang] = $langUrl;
}
} else {
$locale[$langPrefix] = $langUlr;
$locale[$langPrefix] = $langUrl;
}
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "outeredge/magento-hreflang-alternateurl-module",
"description": "Magento Alternate URL & hreflang Module by outer/edge",
"type": "magento2-module",
"version": "1.1.1",
"version": "1.1.2",
"license": [
"MIT"
],
Expand Down

0 comments on commit 9e0c0c4

Please sign in to comment.