Skip to content

Instantly share code, notes, and snippets.

@ClaudiuCreanga
Created March 25, 2017 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ClaudiuCreanga/a835312a0304ada983bf821657cc694b to your computer and use it in GitHub Desktop.
Save ClaudiuCreanga/a835312a0304ada983bf821657cc694b to your computer and use it in GitHub Desktop.
magento2 get list of stores
<?php
use Magento\Framework\App\Bootstrap;
require '../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$productCollection = $obj->create('Magento\Store\Model\StoreManagerInterface');
function getStores($withDefault = false, $codeKey = false)
{
$stores = [];
foreach ($this->storeRepository->getList() as $store) {
if (!$withDefault && $store->getId() == 0) {
continue;
}
if ($codeKey) {
$stores[$store->getCode()] = $store;
} else {
$stores[$store->getId()] = $store;
}
}
return $stores;
}
$da = $productCollection->getStores($withDefault = true);
?>
<ul class="list-stores">
<?php
foreach ($da as $nu): $url = parse_url($nu->getBaseUrl()); ?>
<li class="individual-store">
<a href='<?php echo $url["path"]; ?> ' title='<?php echo $nu->getName(); ?>
'>
<?php echo $nu->getName(); ?>
</a></li>
<?php endforeach;
?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment