Question

How in custom created module to make the logic of receiving and displaying all links to stores. The display should be on the homepage.

I tried the solution that suggested Google search, but I can’t finish the solution, because I don’t know how to put all the stores on the homepage.

public function __construct(
...,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
...
     $this->_storeManager = $storeManager;
}
....
private function getStoreData(){
    $storeManagerDataList = $this->_storeManager->getStores();
     $options = array();

     foreach ($storeManagerDataList as $key => $value) {
               $options[] = ['label' => $value['name'].' - '.$value['code'], 'value' => $key];
     }
     return $options;
}
Was it helpful?

Solution

You can use default block for this Magento\Store\Block\Switcher

Call phtml file with above block with below code

<?php foreach ($block->getStores() as $store): ?>
    <a href="#" data-post='<?= $block->getTargetStorePostData($store) ?>'>
        <?= $block->escapeHtml($store->getName()) ?>
    </a>
<?php endforeach; ?>

Note: Not only URL can switch the store, you need to set it in cookie using data-post attribute.

Call phtml file from xml like:

<block class="Magento\Store\Block\Switcher" name="all_stores" template="Vendor_Module::stores.phtml"/>

Call phtml file from CMS page like:

{{block class="\Magento\Store\Block\Switcher" name="all_stores" template="Vendor_Module::stores.phtml"}}

Call you phtml from theme.

app/design/frontend/Vendor/Module/Magento_Theme/templates/stores.phtml

anc call from cms block

{{block class="\Magento\Store\Block\Switcher" name="all_stores" template="Magento_Theme::stores.phtml"}}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top