Description
Summary (*)
I have the config "store code in URL" set to "yes" but when i use the method "getCurrentUrl" on a Store type variable i get the current URL but with the parameter "___store=[code]" in it, if the current store is not the one requested in the URL.
I want to use the getCurrentUrl method in order to redirect the user to the correct store based on some custom logic (like the browser language), but i don't want any additional parameters to be in the URL.
Form what i found out by looking around in the code, the parameter "___store" is mandatory when the config "use store code in URL" is set to "no" but not when it's set to "yes", as in my case. The store code is already in the URL (as set in system config) and it's not necessary to add any other parameters in the URL to remark this.
Examples (*)
<?php
namespace Fonderia\StoreAutoRedirect\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\Store;
use Magento\Store\Api\StoreRepositoryInterface;
class RedirectObserver implements ObserverInterface
{
/**
* @var StoreRepositoryInterface
*/
private $storeRepository;
public function __construct(StoreRepositoryInterface $storeRepository) {
$this->storeRepository = $storeRepository;
}
public function execute(Observer $observer)
{
$storeCode = '[your store code]';
/** @var Store $store */
$store = $this->storeRepository->get($storeCode);
$url = $store->getCurrentUrl(true);
// ...
}
}
Proposed solution
Not a solution but this is what i found out by debugging.
At this line of code you can clearly see that it's applying the right logic. But if you try to debug you'll see that at this point of the code the variable $storeUrl has already the parameter "___store" set in it, because it's in this line that it do this. By going to the getUrl method implementation you'll found out that if the current store is not the one requested in the URL it'll add the "_scope_to_url" param at this line. This flag will eventually cause the problem because it will end up at this line where i think the wrong logic is applied, it's the opposite as the one that i pointed out initially in this section.