0

I have two stores Sweeden and France and two storeviews for each sweedish,english and french,english respectively.

i.e

Store Sweeden(SE) has two Storeviews Sweedish(se) and English(se_en)

Store France(FR) has two Storeviews French(fr) and English(fr_en)

I'm trying to get the list of stores collection in the frontend.

But I'm getting storeviews details directly.

1-default
0-admin
2-se
3-en
4-fr
5-fr_en

This is the code I used to get stores collection

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->create("\Magento\Store\Model\StoreManagerInterface");
$stores = $storeManager->getStores(true, false);
foreach($stores as $store){
echo $store->getId()."-".$store->getCode()."<br>";
//echo $store->getAll();
}

Thanks in Advance.

  • please check my answer. – Rohan Hapani Oct 30 '18 at 13:17
1

You need to inject \Magento\Store\Model\StoreManagerInterface class in your construct. Try to use this below code.

protected $_storeManager;
public function __construct(
    ................
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    ................
) {
    ................
    $this->_storeManager = $storeManager;
    ................
}
public function yourFunction()
{
    $groups = $this->_storeManager->getWebsite()->getGroups();
    $storeName = [];
    $storeViewName = [];
    foreach ($groups as $key => $group) {
        $storeName[] = $group->getName(); // get store name
        $stores = $group->getStores();
        foreach ($stores as $store) {
            $storeViewName[] = $store->getName(); // get store view name
        }
    }
    print_r($storeViewName);
    print_r($storeName);
}

Clean cache and check output.

  • HI @Rohan May I know from where I can get that $groups in your code.Thanks – anil Oct 30 '18 at 14:38
  • Oh sorry. I forgot to add it. – Rohan Hapani Oct 30 '18 at 15:16
  • Please check my updated answer. – Rohan Hapani Oct 30 '18 at 15:22
  • HI @Rohan Thanks for the update.I'll check and let you know. – anil Oct 31 '18 at 11:38
  • Is it working for you ? Please accpet and upvote answer so other user can also use this answer :) – Rohan Hapani Nov 1 '18 at 4:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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