1

i want to pass store code to the url but not with "Add Store Code to Urls" inbuilt feature because it gives warning " Warning! When using Store Code in URLs, in some cases system may not work properly if URLs without Store Codes are specified in the third-party services (e.g. PayPal etc.)." So please give me any other solution that can be implement with the admin pannel

2
  • Hi, first I wanted to know you are using multi store or not? Commented Dec 7, 2022 at 11:50
  • yes , i'm using
    – user110069
    Commented Dec 8, 2022 at 11:13

2 Answers 2

1

For multi store, you need to create stores

Stores -> Settings -> All Stores

Create Store View, Store(here you can define store code) and Create Website(for website information)

Then Go to Stores -> Settings -> Configuration Here select the store from Scope(left side of side menus) then,

Go to General -> Web -> Base URLs

Write yours store urls(according to store) for Base URL, Base Link URL, Base URL for Static Vire Files,Base URL for User Media Files.

Do the same for all store and check your store website, it will work.

Hope this will work for you, because multistore we are already using with same concept.

Thank You

1

Create an View Model :

<?php

namespace Vendor\Module\ViewModel;

class AllWebsite implements \Magento\Framework\View\Element\Block\ArgumentInterface
{
    public function __construct(
        \Magento\Store\Model\ResourceModel\Website\CollectionFactory $websiteCollectionFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->_websiteCollectionFactory = $websiteCollectionFactory;
        $this->_storeManager = $storeManager;
    }

     /**
     * Retrieve websites collection of system
     *
     * @return Website Collection
     */
    public function getWebsiteCollection()
    {
        $collection = $this->_websiteCollectionFactory->create();
        return $collection;
    }

    /**
     * Retrieve Baseurl
     *
     * @return Baseurl
     */
    public function getbaseUrl()
    {
        return $this->_storeManager->getStore()->getBaseUrl();
    }
}

Add a code in default.xml

<referenceBlock name="store_switcher">
    <arguments>
       <argument name="viewModel" xsi:type="object">Vendor\Module\ViewModel\AllWebsite</argument>
    </arguments>
</referenceBlock>

Create file app/design/frontend/Vendor/Theme/Magento_Store/templates/switch/stores.phtml

  <?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/** @var \Magento\Store\Block\Switcher $block */
$viewModel = $block->getData('viewModel');
$websites  = $viewModel->getWebsiteCollection();
$baseUrl   = $viewModel->getbaseUrl();
?>
<?php if (count($block->getGroups())>1) : ?>
<div class="switcher store switcher-store" id="switcher-store">
    <strong class="label switcher-label"><span><?= $block->escapeHtml(__('Select Store')) ?></span></strong>
    <div class="actions dropdown options switcher-options">
        <?php foreach ($block->getGroups() as $_group) : ?>
            <?php if ($_group->getId() == $block->getCurrentGroupId()) : ?>
                <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><?= $block->escapeHtml($_group->getName()) ?></span>
                    </strong>
                </div>
            <?php endif; ?>
        <?php endforeach; ?>
        <ul class="dropdown switcher-dropdown" data-target="dropdown">
            <?php foreach ($websites as $website) : ?>
                    <li class="switcher-option">
                        <a href="<?= $baseUrl.$website->getDefaultStore()->getCode(); ?>">
                            <?= $block->escapeHtml($website->getName()) ?>
                        </a>
                    </li>
            <?php endforeach; ?>
        </ul>
    </div>
</div>
<?php endif; ?>
4
  • where add this code in stores.phtml file?? , should i replace or add it??? if add then what will be the addone position in the code?
    – user110069
    Commented Dec 9, 2022 at 10:37
  • On line no 28 . i.imgur.com/3REVykg.png Commented Dec 9, 2022 at 12:32
  • sorry this code is not working on my side. can you tell me that where is the value of $websites coming from?? because this is not using in my core file. in my core file uses $block->getGroups() instead of $websites ... if i replace it from $block->getGroups() to $websites , then it is not working , please help
    – user110069
    Commented Dec 12, 2022 at 4:52
  • Update my answer Commented Dec 12, 2022 at 12:31

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.