Back to Top

How to load Store specific Category in Magento 2

Updated 1 March 2024

In Magento 2 you can set category data such as name, description different for each store view. In this blog I will explain how you can load store specific category or how you can get the category data per store.

For example I have created a category with name “New Category” and set the name as “Latest Category” for second store.

When I loaded the category and printed the data, it has default data as shown below,

Default data

 

To get the data for a specific store in our case second store we need to use setStoreId($storeId) method as in the below code.

Searching for an experienced
Magento 2 Company ?
Find out More
public function __construct(
    .....
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    .....
) {
    .....
    $this->categoryFactory = $categoryFactory;
    .....
}
  
public function execute()
{
    .....
    $category = $this->categoryFactory->create()->setStoreId(2)->load(4);   //here 2 is the store id of the second store
    .....
}

Now when we check the data it will show the data set for second store,

Second store data

 

In the same way we can load the whole collection of category for a specific store,

public function execute()
{
    .....
    $categories = $this->categoryFactory->create()->setStoreId(2)->getCollection();     //here 2 is the store id of the second store
    .....
}

Thank you for checking this blog.

Please let me know if you find any issue.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home