First I have refreshed the reports on best sellers on the admin panel it's giving me updated data on bestseller tables.

I get all the products id's of the best seller but the issue is


These 3 tables have older products as well which has been removed from catalog_product_entity so it gives me an error every time when I try to load products from the product ids of sales_bestsellers_aggregated_daily/sales_bestsellers_aggregated_monthly/sales_bestsellers_aggregated_yearly that product doesn't exist.

So what I did, I added a join condition on this BestSeller collection to catalog_product_entity on product id equality.

protected $_reportProductCollectionFactory;

public function __construct(\Magento\Reports\Model\ResourceModel\Report\Collection\Factory $reportProductCollectionFactory) {
           $this->_reportProductCollectionFactory = $reportProductCollectionFactory;
$collection = $this->_reportProductCollectionFactory->create('Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection');
$collection->getSelect()->join(array('cev' => 'catalog_product_entity'), 'cev.entity_id = product_id');

this doesn't give me all the products. what is wrong in that how can I exclude those bestseller products which are not in catalog_product_entity / or it has been deleted permanently from our catalog? (Answer: so I join Best Seller collection with catalog_product_entity)

And further, I have to add a filter on this collection for the manufacturer, to show only that best seller products for which product page brand belongs to, i.e, how to apply the filter on bestseller collection for particular brand/manufacturer.

I cant apply addAttributeToFilter to Best Seller collection.

Any solution?

I want something like this:--

$toDate =  date("y-m-d");
$fromDate = date("y-m-d",strtotime("-6 month"));

$bestSellercollection = $this->_reportProductCollectionFactory->create('Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection');
$bestSellercollection->getSelect()->join(array('cev' => 'catalog_product_entity'), 'cev.entity_id = product_id'); 
$bestSellercollection->addAttributeToFilter('manufacturer', $brandId);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.