Menu Close
    Hire Us Request Quote Reading List Switch to Dark Mode

    How to load Store specific Category in Magento 2

    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,

     

    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.

    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,

     

     

    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.

    . . .

    Comment

    Add Your Comment

    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close