How can I get store specific products. Mysql query will also work. Can some one help me on this. Thanks.
2 Answers
You can use this
<?php
namespace Vendor\Module\Block;
class Product extends \Magento\Framework\View\Element\Template
{
protected $_productCollectionFactory;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
array $data = []
)
{
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct($context, $data);
}
public function getProductCollection()
{
$storeId = 1; //Store ID
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addStoreFilter($storeId);
return $collection;
}
}
?>
change your Store ID
.
Hope this will work!
-
I tried it in same way but when I am trying to print query it is giving me this result SELECT
e
.* FROMcatalog_product_entity
ASe
INNER JOINcatalog_product_website
ASproduct_website
ON product_website.product_id = e.entity_id AND product_website.website_id = '1' WHERE (e.created_in <= 1) AND (e.updated_in > 1) It is checking with website id and not store id. Commented Nov 19, 2019 at 10:58
Hope this might will help you.
<?php
namespace Vendor\Module\Block;
class Product extends \Magento\Framework\View\Element\Template
{
protected $productFactory;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Catalog\Model\ProductFactory $productFactory,
array $data = []
)
{
$this->productFactory = $productFactory;
parent::__construct($context, $data);
}
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;
}
}