public function getProductCollection()
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$filterableAttributes = $objectManager->create(MagentoCatalogModelLayerCategoryFilterableAttributeList::class);
$attributes = $filterableAttributes->getList();
$price=$this->getRequest()->getParam('price');
$cat=$this->getRequest()->getParam('cat');
$sortby=$this->getRequest()->getParam('product_list_order');
if (empty($sortby)) {
$sortby='position';
}
$listdesc=$this->getRequest()->getParam('product_list_dir');
$page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
//get values of current limit
$pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 3;
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->setOrder('title','ASC');
$collection->setPageSize($pageSize);
$collection->setCurPage($page);
foreach ($attributes as $attributes1 ) {
$attributefrontend = $attributes1->getFrontendInput();
if (!empty($_REQUEST[$attributes1->getAttributeCode()]) && $attributefrontend=="select") {
$collection->addAttributeToFilter($attributes1->getAttributeCode(), $_REQUEST[$attributes1->getAttributeCode()]);
}
if (!empty($_REQUEST[$attributes1->getAttributeCode()]) && $attributefrontend=="multiselect") {
$collection->addAttributeToFilter($attributes1->getAttributeCode(), array('in' => array((int)$_REQUEST[$attributes1->getAttributeCode()])));
}
if (isset($price) || isset($cat) || isset($_REQUEST[$attributes1->getAttributeCode()]) || isset($sortby)) {
$pricebetween=explode("-",$price);
if (!empty($pricebetween[0])) {
$collection->addAttributeToFilter('price', array('gteq' => $pricebetween[0]));
}
if (!empty($pricebetween[1])) {
$collection->addAttributeToFilter('price', array('lteq' => $pricebetween[1]));
}
if (!empty($cat)) {
$category = $this->_categoryFactory->create()->load($cat);
$collection->addCategoryFilter($category);
$collection->addAttributeToFilter('visibility', MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$collection->addAttributeToFilter('status',MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
}
}
if ($sortby=='position') {
$collection->setOrder($sortby,'ASC');
}
if (!empty($listdesc)) {
$collection->setOrder($sortby,'DESC');
} else {
$collection->setOrder($sortby,'ASC');
}
}
/*echo $collection->getSelect(); */
return $collection;
}
In this collection This bestseller give me the error it’s not working
$collection->getSelect()->joinLeft(
'sales_order_item',
'e.entity_id = sales_order_item.product_id',
array('qty_ordered'=>'SUM(sales_order_item.qty_ordered)'))
->group('e.entity_id')
->order('qty_ordered '.$subject->getCurrentDirection());