Question:

How use Pagination in Custom Product Page

Delilah: 20 May 2022

enter image description here

I display custom products in page. use controller ,block and .phtml files define block and template in xml file.

Products are showing correctly i use template module/catalog/view/frontend/template\product\list.phtml .

Now i want to apply pagination and iuse many solution like

how to add pagination on custom collection in Magento 2 (https://magento.stackexchange.com/questions/130405/how-to-add-pagination-on-custom-collection-in-magento-2/130407?noredirect=1#comment389610_130407)

https://meetanshi.com/blog/add-pagination-in-magento-2-custom-collection/#comment-962

https://mage2.pro/t/topic/1035

https://www.fmeextensions.com/blog/how-to-add-magnto-2-pagination-to-custom-collection/

All these solution just show limited products on page but not show next page counting toolbar.

Can anyone help me please ? i already spend too much time

Answer:
Anthony: 20 May 2022

enter image description here

In Your .phtml file :-

<?php if ($block->getPagerHtml()): ?>
    <div class="order-products-toolbar toolbar bottom"><?php echo $block->getPagerHtml(); ?></div>
<?php endif ?>

Block file :-

<?php
namespace Vendor\Module\Block;

use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\View\Element\Template;

class ClassName extends \Magento\Framework\View\Element\Template
{

    protected $YourFactory;

    protected $_storeManager;

    public function __construct(Template\Context $context,
                                StoreManagerInterface $storeManager,
                                \Vendor\Module\Model\YourFactory $YourFactory,
                                array $data = [])
    {
        $this->YourFactory=$YourFactory;
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }

    public function _prepareLayout()
    {
        parent::_prepareLayout();
        $this->pageConfig->getTitle()->set(__('Your Title'));

        if ($this->getTestimonials()) {
            $pager = $this->getLayout()->createBlock(
                'Magento\Theme\Block\Html\Pager',
                'name.pager'
            )->setAvailableLimit(array(12=>12,24=>24,36=>36,72=>72))
                ->setShowPerPage(true)->setCollection(
                    $this->getYourFunction()
                );
            $this->setChild('pager', $pager);
            $this->getTestimonials()->load();
        }
        return $this;
    }
    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }

    public function getYourFunction()
    {
        $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
        //get values of current limit
        $pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest
        ()->getParam('limit') : 12;
        $collection = $this->yourFactory->create()->getCollection();
        $collection->setPageSize($pageSize);
        $collection->setCurPage($page)->getData();
        return $collection;
    }

}