Created
March 25, 2017 12:25
-
-
Save ClaudiuCreanga/a835312a0304ada983bf821657cc694b to your computer and use it in GitHub Desktop.
magento2 get list of stores
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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