these days I've faced a problem: I needed to to sort products by sales. So, first of all I decided to add this field to collection of products and the order it using method $collection->setOrder()
.
So, here is my solution:
$collection = $this->_productCollection->create()
->joinField(
'qty_ordered',
'sales_bestsellers_aggregated_yearly',
'qty_ordered',
'product_id=entity_id',
array('store_id' => $this->_storeManager->getStore()->getId()),
'left'
)
->addAttributeToSelect('*')
->setOrder('qty_ordered');
I've posted it cause I think it would be helpful for you. And also I am not sure that it is the best solution.
Maybe someone know better solutions?