1

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 ?>
  • Any error in this? also check in html sometime pagination are set hidden. – Qaisar Satti Nov 9 '17 at 6:32
  • No error ........ No Hidden – Magento 2 Nov 9 '17 at 6:37
2

you are using return parent::_prepareLayout(); after that code is not run.

change this

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;
   }

to

protected function _prepareLayout()
   {
        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;
   }

Now here is you solution

namespace QaisarSatti\HelloWorld\Block;

class HelloWorld 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);
   }

   protected function _prepareLayout()
   {
        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(){

      $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;

      $pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 10;
       $collection = $this->_collectionFactory->create()
                    ->setModel('Magento\Catalog\Model\Product')
                    ->setPeriod('month')

                   ;
   $bestSeller = $collection->getColumnValues('product_id');   

   $newCollection = $this->_productsFactory->create()->addAttributeToSelect('*')->addFieldToFilter('entity_id',array('in',$bestSeller))->setPageSize($pageSize)->setCurPage($page);   

       return $newCollection;
   }


}

phtml code

<?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->getName();?></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 ?>

Reference

GitHub

  • Now page title comming but no pager – Magento 2 Nov 9 '17 at 7:20
  • @Magento2 i added the solution for you it is tested and working. – Qaisar Satti Nov 9 '17 at 7:38
  • Thanks it's working how to get all information about product like image qty etc... – Magento 2 Nov 9 '17 at 9:27
  • @Magento2 use ->addAttributeToSelect('*') in collection already updated the code. – Qaisar Satti Nov 9 '17 at 11:16
  • i have some issue in custom page collection magento.stackexchange.com/questions/200820/… – Magento 2 Nov 9 '17 at 19:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.