0

I have implemented pagination with limit & offset but when I am fetching the collection total after applying multiple filters then it's giving the wrong total count.

        $layer = $layerResolver->get();
        $layer->setCurrentCategory($categoryData->getId());
        $allFilters = $filterList->getFilters($layer);
        $collection = $layer->getProductCollection();
        $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
        $collection->addAttributeToFilter('visibility',4);
        //Code for filters.
        if($category != '')
        {
          $cat_filter_ids = array();  
          $cat_filter_ids = explode(',', $category); 
          $collection->addCategoriesFilter(['in' => $cat_filter_ids]);                
        }           
        if($gender != '')
        {                                                
          $collection->addAttributeToFilter('gender_pim', array('in' => explode(',', $gender)));
        }
        if($brand != '')
        {
          $collection->addAttributeToFilter('manufacturer_pim', array('in' => explode(',', $brand)));                              
        }                        
        if($price != '')
        {
          $final_price = array();  
          $final_price = explode('-', $price);
          $pricefrom = $final_price[0];
          $priceto = $final_price[1];
          $collection->getSelect()->where("price_index.min_price >= ".$pricefrom)
             ->where("price_index.min_price <= ".$priceto);
        }            
        $collection->getSelect()->group('e.entity_id');

        //Code for sorting.      
        if($product_list_order == 'price-desc')
        {
         $collection->setOrder('price', 'desc');
        }
        elseif($product_list_order == 'price-asc')
        {
         $collection->setOrder('price', 'asc');   
        }
        elseif($product_list_order == 'newin')
        {
          //$collection->getSelect->order('entity_id desc');  
          //$collection->setOrder('created_at', 'desc');   
        }
        elseif($product_list_order == 'bestseller')
        {
          $collection->setOrder('bestselling2', 'desc');   
        }
        elseif($product_list_order == 'featured')
        {
          $collection->setOrder('featured2', 'desc');   
        }
        //echo $collection->count();exit;                                   
        //Code for pagination.
        $limit = $this->scopeConfig->getValue('catalog/frontend/list_per_page');
        $p = $p?$p:1;            
        $offset = ($p != 1)?((($p-1)*$limit)+1):$p-1;
        $pageSize = $limit?$limit:20;             
        $collection->getSelect()->limit($pageSize,$offset);
        $collection->getSize(); //After filtering giving wrong count.

1 Answer 1

0

Finally resolved the collection wrong count issue by adding a collection clear method. Just we need to add after pagination or limit like the below example. Now working fine with pagination and also fetching correct total after applying pagination to the collection.

$collection->getSelect()->limit($pageSize,$offset);
$collection->clear(); //This clear collection & give you correct collection total.**  
$collection->getSize(); //Now you will get correct collection total even if you have pagination code.

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.