I get best seller product for all category using below code
public function __construct(\Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory
) {
$this->_collectionFactory = $collectionFactory;
}
public function getBestSellerData()
{
$collection = $this->_collectionFactory->create()->setModel(
'Magento\Catalog\Model\Product'
);
return $collection;
}
How can i add category filter for collection.
in magento 1 we can do like that
$catNum = 7; //The number of the category you want to load
$category = Mage::getModel('catalog/category')->load($catNum);
$products = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToSelect('*')
->setStoreId($storeId)
->addStoreFilter($storeId)
->addCategoryFilter($category);
Best Answer
The only product information returned from the reports bestseller collection is as follows:
hence you will need to do a join and then filter like so
You could also do a join and filter by category
Which will give you below:
So you can
addFieldToFilter()
based on category_id.