Skip to main content
edited tags
Link
Qaisar Satti
  • 32.5k
  • 18
  • 87
  • 137
Source Link
Magento 2
  • 3.8k
  • 7
  • 55
  • 107

Magento 2 pager not working custom collection

In My custom Collection Pager not working Index.php

<?php

namespace Test\CustomProduct\Block\Index;


class Index extends \Magento\Framework\View\Element\Template {
    protected $_coreRegistry = null;
    protected $_collectionFactory;
    protected $_productsFactory;
 
   public function __construct(
       \Magento\Backend\Block\Template\Context $context,
       \Magento\Framework\Registry $registry,
      \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,
        \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory,
       array $data = []
   ) {
       $this->_collectionFactory = $collectionFactory;
       $this->_coreRegistry = $registry;
       $this->_productsFactory = $productsFactory;
       parent::__construct($context, $data);
   }
 
   public function _prepareLayout()
   {
       return parent::_prepareLayout();
       $this->pageConfig->getTitle()->set(__('Best Seller'));

       if ($this->getBestSellerData()) {
            $pager = $this->getLayout()->createBlock('Magento\Theme\Block\Html\Pager','vlc.history.pager')->setAvailableLimit(array(5=>5,10=>10,15=>15,20=>20));
            $pager->setLimit(5)->setShowPerPage(true);
            $pager->setCollection($this->getBestSellerData());
            $this->setChild('pager', $pager);
            $this->getBestSellerData()->load();
        }
        return $this;
   }
   public function getPagerHtml(){
        return $this->getChildHtml('pager');
    }


   public function getBestSellerData(){
       $collection = $this->_collectionFactory->create()
                    ->setModel('Magento\Catalog\Model\Product')
              ->setPageSize(100)
                    ->setPeriod('month');
                    //->setPeriod('day');
                    //->setPeriod('year');
 
       return $collection;
   }

   public function getMostviewdData(){
        $currentStoreId = $this->_storeManager->getStore()->getId();
        $collection = $this->_productsFactory->create()
        ->addAttributeToSelect(
            '*'
        )->addViewsCount()->setStoreId(
                $currentStoreId
        )->addStoreFilter(
                $currentStoreId
        );
        $items = $collection->getItems();
        return $collection;
        
    }

    public function getNewProductCollection(){
        $collection = $this->_productsFactory->create()
        ->addAttributeToSelect('*')
        ->setPageSize(5);
        /*Add you filters here as suggested by Amit and and Manashvi */
        return $collection;
    }



}

.phtml

<?php /* Best Seller Collection */?>
    <?php 
        $bestSeller =  $block->getBestSellerData();
        //echo count($bestSeller);?>
        <h1>Best Seller Collection.....</h1>
        <ul>
            <?php foreach ($bestSeller as $value) {?>
                <li><?php echo $value['product_name'];?></li>
            <?php } ?>
        </ul>
<?php /* Best Seller Collection */?>
<?php if ($block->getPagerHtml()): ?>
        <div class="order-products-toolbar toolbar bottom"><?php echo $block->getPagerHtml(); ?></div>
    <?php endif ?>