Skip to content

Calling getCurrentUrl on Store will wrongly add "___store" parameter #18941

Closed
@LucaGallinari

Description

@LucaGallinari

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.

Metadata

Metadata

Assignees

Labels

Component: StoreEvent: mm18nycFixed in 2.2.xThe issue has been fixed in 2.2 release lineFixed in 2.3.xThe issue has been fixed in 2.3 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releaseReproduced on 2.3.xThe issue has been reproduced on latest 2.3 release

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions