0

I have recently created a my site 4 different multi-website and as like switcher in store views I want to make switcher on my multi site to show customer to switch our other country multi website. it possible this? can anyone please help me this.Many Thanks advance.

1 Answer 1

0

Sure, here's an example of how you can implement a basic multi-website switcher in Magento. This example assumes you have already set up multiple websites in your Magento installation.

First, create a custom module to handle the switcher functionality:

  1. Create Module Files: In your Magento installation, create the necessary module files. For example, if your module is named Custom_WebsiteSwitcher, create the following files:

    • app/code/Custom/WebsiteSwitcher/etc/module.xml
    • app/code/Custom/WebsiteSwitcher/registration.php
    • app/code/Custom/WebsiteSwitcher/Helper/Data.php
    • app/code/Custom/WebsiteSwitcher/view/frontend/templates/website_switcher.phtml
  2. Module Configuration: Add the following code to app/code/Custom/WebsiteSwitcher/etc/module.xml:

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="Custom_WebsiteSwitcher" setup_version="1.0.0">
            <sequence>
                <module name="Magento_Store"/>
                <module name="Magento_Theme"/>
            </sequence>
        </module>
    </config>
    
  3. Helper Class: Create a helper class app/code/Custom/WebsiteSwitcher/Helper/Data.php with the following code:

    <?php
    namespace Custom\WebsiteSwitcher\Helper;
    
    use Magento\Framework\App\Helper\AbstractHelper;
    use Magento\Store\Model\StoreManagerInterface;
    
    class Data extends AbstractHelper
    {
        protected $storeManager;
    
        public function __construct(
            StoreManagerInterface $storeManager
        ) {
            $this->storeManager = $storeManager;
        }
    
        public function getWebsites()
        {
            return $this->storeManager->getWebsites();
        }
    }
    
  4. Template File: Create a template file app/code/Custom/WebsiteSwitcher/view/frontend/templates/website_switcher.phtml with the following code:

    <div class="website-switcher">
        <label for="website-switcher">Select Website:</label>
        <select id="website-switcher" onchange="switchWebsite(this)">
            <?php foreach ($block->getWebsites() as $website): ?>
                <option value="<?= $website->getDefaultStore()->getBaseUrl() ?>">
                    <?= $website->getName() ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    
    <script>
        function switchWebsite(element) {
            var baseUrl = element.value;
            window.location.href = baseUrl;
        }
    </script>
    
  5. Enable Module: After creating these files, enable the module by running the following commands in the Magento root directory:

    bin/magento module:enable Custom_WebsiteSwitcher
    bin/magento setup:upgrade
    
  6. Place Switcher in Layout: Finally, place the switcher in your desired location within your Magento theme's layout file (e.g., app/design/frontend/YourVendor/YourTheme/Magento_Theme/layout/default.xml):

    <referenceContainer name="header.panel">
        <block class="Custom\WebsiteSwitcher\Block\Switcher" name="website.switcher" template="Custom_WebsiteSwitcher::website_switcher.phtml" />
    </referenceContainer>
    

After completing these steps, clear the Magento cache and test the website switcher on your frontend. The switcher should display a dropdown with your website names, and when a user selects a website, it should redirect them to the corresponding base URL of that website. Adjust the code as needed based on your specific requirements and Magento version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.