Пейджер Magento 2 не работает пользовательская коллекция


В Моей пользовательской коллекции пейджер не работает 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 ?>
Author: Qaisar Satti, 2017-11-09

1 answers

Вы используете return parent::_prepareLayout(); после того, как этот код не будет запущен.

Измените это

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

До

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

Теперь вот вам решение

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

<?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 ?>

Ссылка

Гитхаб

 2
Author: Qaisar Satti, 2017-11-09 11:16:15