Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.