Magento Tutorials

How to get Magento 2 all stores programmatically (Retrieve a list of all stores)

Hello, Magento Buddies!

As we all know that the default Magento offers one Magento website, store, and store views. Thereafter, multiple websites, stores, and their store views can be built depending on the requirements.

So, to fetch a list of all the stores, you are required to perform only a few pieces of code which are right hereinafter. The method is pretty easy. Also, to know how you can quickly set up multiple stores in Magento 2, where I have given a step-by-step guide to configuring multiple stores in Magento 2.

Let’s Commence!

Steps To Retrieve a List Of All Stores Programmatically

Step 1: Create a Store.php file in the below path.

app\code\Vendor\Extension\Block\

Now, add the below-given code:

<?php

namespace Vendor\Extension\Block;

use Magento\Framework\View\Element\Template;

use Magento\Backend\Block\Template\Context;

use Magento\Store\Api\StoreRepositoryInterface;

class Store extends Template

{

    private $storeRepository;

    public function __construct(Context $context, StoreRepositoryInterface $storeRepository, array $data = [])

    {

        $this->storeRepository = $storeRepository;

        parent::__construct($context, $data);

    }

    public function Storelist()

    {

        return $this->storeRepository->getList();

    }

}

Step 2: Next, add the below code in your phtml file:

<?php

$all_stores_list = $block->Storelist();

foreach ($all_stores_list as $store) {

    print_r($store->getSortOrder()); // get Sort Order

    print_r($store->getIsActive()); // get is active or not (0 (Disabled) / 1 (Enabled) )

    print_r($store->getName()); // get Store View

    print_r($store->getStoreId()); // get Store id

    print_r($store->getCode()); //  get Store Code

    print_r($store->getGroupId()); // get group id

}

?>

And, it’s done!

Wrap Up!

Integrate the above code appropriately and you will surely get the desired output to get a list of all the Magento 2 stores.

Grab the Magento Multi-Store & Website Setup Service and get ready with multiple stores and websites from a single admin panel. 

Happy Coding!

Click to rate this post!
[Total: 8 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago