I'm using the following code to switch websites.
app/design/frontend/[Vendor]/{Child_theme}/Magento_Store/templates/switch/languages.phtml
<?php
/** @var \Magento\Store\Block\Switcher $block */
$viewModel = $block->getData('view_model');
$websites = $viewModel->getWebsites();
$websiteid = $viewModel->getWebsite()->getWebsiteId();
?>
<div class="switcher store switcher-store" id="switcher-store">
<strong class="label switcher-label"><span><?php echo __('Choose your Country') ?></span></strong>
<div class="actions dropdown options switcher-options">
<?php foreach ($websites as $website): ?>
<?php if ($websiteid == $website->getId()): ?>
<div class="action toggle switcher-trigger"
role="button"
tabindex="0"
data-mage-init='{"dropdown":{}}'
data-toggle="dropdown"
data-trigger-keypress-button="true"
id="switcher-store-trigger">
<strong>
<span class="<?php echo str_replace(' ', '-', $block->escapeHtml($website->getName())) ?>">
<strong><a><?php echo $block->escapeHtml($website->getName()) ?></a></strong>
</span>
</strong>
</div>
<?php endif; ?>
<?php endforeach; ?>
<ul class="dropdown switcher-dropdown" data-target="dropdown">
<?php foreach ($websites as $website): ?>
<?php if (!($websiteid == $website->getId())): ?>
<li class="switcher-option view">
<span class="flag-icon <?php echo str_replace(' ', '-', $block->escapeHtml($website->getName())) ?>">
<a href='<?php echo $website->getDefaultStore()->getBaseUrl() ?>'>
<?php echo $block->escapeHtml($website->getName()) ?>
</a>
</span>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
</div>
However, when a product page is accessed on a particular website and then the website is switched it goes back to the base url of the website switched to.
For example
Different websites are
- xyz.com/us
- xyz.com/uk
- xyz.com/eu
URL that is accessed is xyz.com/uk/apple.html and say you switch to the US website, it goes to xyz.com/us instead of xyz.com/us/apple.html
How can I get it to switch to the same page but on the other website.