1

How can I get store specific products. Mysql query will also work. Can some one help me on this. Thanks.

1

2 Answers 2

1

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!

1
  • I tried it in same way but when I am trying to print query it is giving me this result SELECT e.* FROM catalog_product_entity AS e INNER JOIN catalog_product_website AS product_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. Nov 19, 2019 at 10:58
1

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;
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.