1

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;
}
5
  • How many website you have? Nov 1, 2019 at 8:33
  • @Ranganathan 5, but in the future, I want to automatically add there as new stores arrive Nov 1, 2019 at 9:05
  • 5 websites and each website having stores? Nov 1, 2019 at 9:12
  • @Ranganathan no, no, I have a main site and it has Main Website Store -> Store View1, Store View2, Store View3 ... Nov 1, 2019 at 9:36
  • got it will update my answer Nov 1, 2019 at 9:47

1 Answer 1

1

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"}}
2
  • I'm sorry I didn’t answer earlier. I understood correctly, if I insert your example in cms block, will I get all the links to my stores? Nov 3, 2019 at 13:01
  • No problem....Yes.....You can place your phtml in you theme and call it from cms block. I updated the answer. Nov 4, 2019 at 4:21

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.