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

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

    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.

    Happy coding 👨 💻

    . . .

    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