Skip to main content
Bounty Ended with 25 reputation awarded by CommunityBot
improves formatting & fixs typo
Source Link
Siarhey Uchukhlebau
  • 16.2k
  • 11
  • 55
  • 88

In your block file add below code for add pagination:

public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Catalog\Model\ProductFactory $productFactory,
    \Magento\Framework\Url\Helper\Data $urlHelper,
    array $data = []
    ) {
        $this->_productFactory = $productFactory;
        $this->urlHelper = $urlHelper;
        parent::__construct($context, $data);
 
        
        // Get your custom collection here

        $collection = $this->getCustomCOllection>getCustomCollection();

        $this->setCollection($collection);
    }

public function _prepareLayout()
    {
       
        parent::_prepareLayout();
        if ($this->getCollection()) {

            // create pager block for collection 

            $toolbar = $this->getToolbarBlock();

            $pager = $this->getLayout()->createBlock(
                            'Magento\Theme\Block\Html\Pager', 'list.pager'
                    )->setCollection(
                    $this->getCollection() // assign collection to pager
            );
            $toolbar->setChild('product_list_toolbar_pager', $pager); // set pager block in layout
            // called prepare sortable parameters
            $collection = $this->getCollection();

            // use sortable parameters
            $orders = $this->getAvailableOrders();

            if ($orders) {
                $toolbar->setAvailableOrders($orders);
            }
            $sort = $this->getSortBy();
            if ($sort) {
                $toolbar->setDefaultOrder($sort);
            }
            $dir = $this->getDefaultDirection();
            if ($dir) {
                $toolbar->setDefaultDirection($dir);
            }
            $modes = $this->getModes();
            if ($modes) {
                $toolbar->setModes($modes);
            }
            $toolbar->setCollection($collection);

            $this->setChild('toolbar', $toolbar);
            $this->getCollection()->load();
        }

        return $this;
 
    }

    public function getToolbarHtml()
    {
        return $this->getChildHtml('toolbar');
    }

    public function getToolbarBlock()
    {
        $blockName = $this->getToolbarBlockName();
        if ($blockName) {
            $block = $this->getLayout()->getBlock($blockName);
            if ($block) {
                return $block;
            }
        }
        $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));

        return $block;
    }

    public function getMode()
    {
        return $this->getChildBlock('toolbar')->getCurrentMode();
    }

Also define below constantproperty:

inIn phtml file add below code beforbefore and after lising.listing:

In your block file add below code for add pagination

public function __construct(
    \Magento\Catalog\Block\Product\Context $context,  \Magento\Catalog\Model\ProductFactory $productFactory, \Magento\Framework\Url\Helper\Data $urlHelper,  array $data = []
    ) {
        $this->_productFactory = $productFactory;
        $this->urlHelper = $urlHelper;
        parent::__construct($context, $data);
 
        
        // Get your custom collection here

        $collection = $this->getCustomCOllection();

        $this->setCollection($collection);
    }

public function _prepareLayout()
    {
       
        parent::_prepareLayout();
        if ($this->getCollection()) {

            // create pager block for collection 

            $toolbar = $this->getToolbarBlock();

            $pager = $this->getLayout()->createBlock(
                            'Magento\Theme\Block\Html\Pager', 'list.pager'
                    )->setCollection(
                    $this->getCollection() // assign collection to pager
            );
            $toolbar->setChild('product_list_toolbar_pager', $pager); // set pager block in layout
            // called prepare sortable parameters
            $collection = $this->getCollection();

            // use sortable parameters
            $orders = $this->getAvailableOrders();

            if ($orders) {
                $toolbar->setAvailableOrders($orders);
            }
            $sort = $this->getSortBy();
            if ($sort) {
                $toolbar->setDefaultOrder($sort);
            }
            $dir = $this->getDefaultDirection();
            if ($dir) {
                $toolbar->setDefaultDirection($dir);
            }
            $modes = $this->getModes();
            if ($modes) {
                $toolbar->setModes($modes);
            }
            $toolbar->setCollection($collection);

            $this->setChild('toolbar', $toolbar);
            $this->getCollection()->load();
        }

        return $this;
 
    }

    public function getToolbarHtml()
    {
        return $this->getChildHtml('toolbar');
    }

    public function getToolbarBlock()
    {
        $blockName = $this->getToolbarBlockName();
        if ($blockName) {
            $block = $this->getLayout()->getBlock($blockName);
            if ($block) {
                return $block;
            }
        }
        $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));

        return $block;
    }

    public function getMode()
    {
        return $this->getChildBlock('toolbar')->getCurrentMode();
    }

Also define below constant

in phtml file add below code befor and after lising.

In your block file add below code for add pagination:

public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Catalog\Model\ProductFactory $productFactory,
    \Magento\Framework\Url\Helper\Data $urlHelper,
    array $data = []
) {
    $this->_productFactory = $productFactory;
    $this->urlHelper = $urlHelper;
    parent::__construct($context, $data);
    
    // Get your custom collection here

    $collection = $this->getCustomCollection();

    $this->setCollection($collection);
}

public function _prepareLayout()
{

    parent::_prepareLayout();
    if ($this->getCollection()) {

        // create pager block for collection 

        $toolbar = $this->getToolbarBlock();

        $pager = $this->getLayout()->createBlock(
            'Magento\Theme\Block\Html\Pager', 'list.pager'
        )->setCollection(
            $this->getCollection() // assign collection to pager
        );
        $toolbar->setChild('product_list_toolbar_pager', $pager); // set pager block in layout
        // called prepare sortable parameters
        $collection = $this->getCollection();

        // use sortable parameters
        $orders = $this->getAvailableOrders();

        if ($orders) {
            $toolbar->setAvailableOrders($orders);
        }
        $sort = $this->getSortBy();
        if ($sort) {
            $toolbar->setDefaultOrder($sort);
        }
        $dir = $this->getDefaultDirection();
        if ($dir) {
            $toolbar->setDefaultDirection($dir);
        }
        $modes = $this->getModes();
        if ($modes) {
            $toolbar->setModes($modes);
        }
        $toolbar->setCollection($collection);

        $this->setChild('toolbar', $toolbar);
        $this->getCollection()->load();
    }

    return $this;
}

public function getToolbarHtml()
{
    return $this->getChildHtml('toolbar');
}

public function getToolbarBlock()
{
    $blockName = $this->getToolbarBlockName();
    if ($blockName) {
        $block = $this->getLayout()->getBlock($blockName);
        if ($block) {
            return $block;
        }
    }
    $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));

    return $block;
}

public function getMode()
{
    return $this->getChildBlock('toolbar')->getCurrentMode();
}

Also define below property:

In phtml file add below code before and after listing:

Source Link
Jaimin Parikh
  • 2.4k
  • 2
  • 14
  • 27

It seems you have to set pager and sorting in your custom phtml file.

I have followed below steps pr add pagination and sorting.

In your block file add below code for add pagination

public function __construct(
    \Magento\Catalog\Block\Product\Context $context,  \Magento\Catalog\Model\ProductFactory $productFactory, \Magento\Framework\Url\Helper\Data $urlHelper,  array $data = []
    ) {
        $this->_productFactory = $productFactory;
        $this->urlHelper = $urlHelper;
        parent::__construct($context, $data);

        
        // Get your custom collection here

        $collection = $this->getCustomCOllection();

        $this->setCollection($collection);
    }

public function _prepareLayout()
    {
       
        parent::_prepareLayout();
        if ($this->getCollection()) {

            // create pager block for collection 

            $toolbar = $this->getToolbarBlock();

            $pager = $this->getLayout()->createBlock(
                            'Magento\Theme\Block\Html\Pager', 'list.pager'
                    )->setCollection(
                    $this->getCollection() // assign collection to pager
            );
            $toolbar->setChild('product_list_toolbar_pager', $pager); // set pager block in layout
            // called prepare sortable parameters
            $collection = $this->getCollection();

            // use sortable parameters
            $orders = $this->getAvailableOrders();

            if ($orders) {
                $toolbar->setAvailableOrders($orders);
            }
            $sort = $this->getSortBy();
            if ($sort) {
                $toolbar->setDefaultOrder($sort);
            }
            $dir = $this->getDefaultDirection();
            if ($dir) {
                $toolbar->setDefaultDirection($dir);
            }
            $modes = $this->getModes();
            if ($modes) {
                $toolbar->setModes($modes);
            }
            $toolbar->setCollection($collection);

            $this->setChild('toolbar', $toolbar);
            $this->getCollection()->load();
        }

        return $this;

    }

    public function getToolbarHtml()
    {
        return $this->getChildHtml('toolbar');
    }

    public function getToolbarBlock()
    {
        $blockName = $this->getToolbarBlockName();
        if ($blockName) {
            $block = $this->getLayout()->getBlock($blockName);
            if ($block) {
                return $block;
            }
        }
        $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));

        return $block;
    }

    public function getMode()
    {
        return $this->getChildBlock('toolbar')->getCurrentMode();
    }

Also define below constant

protected $_defaultToolbarBlock = 'Magento\Catalog\Block\Product\ProductList\Toolbar';

in phtml file add below code befor and after lising.

<?php echo $block->getToolbarHtml() ?>