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 👨 💻
1 comments