Reading list Switch to dark mode

    How to load Product and Product Collection for a particular Store in Magento 2?

    Updated 4 March 2024

    Magento provide multi-store feature which comes in handy if we want to create store for multiple languages. Magento allows us to set different details for different stores for a single product. So suppose you have created two stores, one for English and one for Hindi then you can set different product name, description and various other details in both language for the same product.

    In this blog I’ll explain how you can load a product model or the products collection for a particular store. Please check out the code given below,

    public function __construct(
        ...
        \Magento\Catalog\Model\ProductFactory $productFactory,
        ...
    ) {
        ...
        $this->productFactory = $productFactory;
        ...
    }
    
    public function getProduct($storeId, $productId) {
        $product = $this->productFactory->create()
                                ->setStoreId($storeId)
                                ->load($productId);
        return $product;
    }
    
    public function getProductCollection($storeId) {
        $productCollection = $this->productFactory->create()
                                        ->setStoreId($storeId)
                                        ->getCollection()
                                        ->addAttributeToSelect('*');
        return $productCollection;
    }

    Here the getProduct function takes the store id and the product id as parameter and it runs the product model for that specific store.

    And the getProductCollection function takes store id as parameter and returns the product collection for that specific store view.

    Thank you for reading the blog. Feel free to comment if you face any issue.

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    Happy coding 👨 💻

    . . .

    Leave a Comment

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


    1 comments

  • Keshav Dussal
  • Back to Top

    Message Sent!

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

    Back to Home