1

I have custom pagination for my custom product collection. I am using Magento's default toolbar on my custom product list page. I want to add my custom pagination in Magento's default product toolbar. Can anyone please suggest an exact solution for adding this?

Block file

<?php

namespace Mymodule\BestsellerSlider\Block;

class Bestseller extends \Magento\Framework\View\Element\Template
{    
protected $_productCollectionFactory;

public function __construct(
    \Magento\Backend\Block\Template\Context $context,  
    \Magento\Store\Model\StoreManagerInterface $storeManager,    
    \Magento\Framework\Pricing\Helper\Data $priceHelper,  
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,        
    array $data = []
)
{    
    $this->_productCollectionFactory = $productCollectionFactory;  
    $this->_priceHelper = $priceHelper;  
    $this->_storeManager = $storeManager;
    parent::__construct($context, $data);
}

protected function _prepareLayout()
{
    if ($this->getProductCollection()) {
        $pager = $this->getLayout()->createBlock(
            'Magento\Theme\Block\Html\Pager',
            'mymodule.bestsellerslider.pager'
        )->setAvailableLimit(array(9=>9,15=>15,30=>30))->setShowPerPage(true)->setCollection(
            $this->getProductCollection()
        );
        $this->setChild('pager', $pager);
        $this->getProductCollection()->load();
    }
    return parent::_prepareLayout();
}

public function getProductCollection()
{
    $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
    $pageSize=($this->getRequest()->getParam('limit')) ? $this->getRequest()->getParam('limit') : 9;
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->addAttributeToFilter('bestseller_sorting',['neq' => 'NULL']);
    $collection->addAttributeToSort('bestseller_sorting','DESC');
    $collection->setPageSize($pageSize);
    $collection->setCurPage($page);
    return $collection;
}

public function getTitle(){
    return __('Best Sellers');
}

public function getBaseUrl(){
    return $this->_storeManager->getStore()->getBaseUrl();
}

public function getProductPrice($price){
    return $this->_priceHelper->currency($price, true, false);
}

public function getPagerHtml()
{
    return $this->getChildHtml('pager');
}
 }

 ?>

Layout file

<?xml version="1.0"?>
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <head>
            <title>Best Sellers</title>
        </head>
        <body>
            <referenceContainer name="page.wrapper">
                <container name="category.top.title.section" htmlTag="div" htmlClass="category-top-title-section" before="main.content" />
            </referenceContainer>
            <move element="page.main.title" destination="category.top.title.section" />
            <referenceBlock name="breadcrumbs">
                <action method="addCrumb">
                    <argument name="crumbName" xsi:type="string">Home</argument>
                    <argument name="crumbInfo" xsi:type="array">
                        <item name="title" xsi:type="string">Home</item>
                        <item name="label" xsi:type="string">Home</item>
                        <item name="link" xsi:type="string">/</item>
                    </argument>
                </action>
                <action method="addCrumb">
                    <argument name="crumbName" xsi:type="string">Bestsellers</argument>
                    <argument name="crumbInfo" xsi:type="array">
                        <item name="title" xsi:type="string">Bestsellers</item>
                        <item name="label" xsi:type="string">Bestsellers</item>
                    </argument>
                </action>
            </referenceBlock>
            <referenceContainer name="content">
                <block class="Magento\Catalog\Block\Product\ListProduct"  name="category.products.list" as="product_list" template="Mymodule_BestsellerSlider::bestseller.phtml" >
                    <block class="Magento\Catalog\Block\Product\ProductList\Toolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml">
                        <block class="Magento\Theme\Block\Html\Pager" name="product_list_toolbar_pager" />
                    </block>
                    <action method="setToolbarBlockName">
                        <argument name="name" xsi:type="string">product_list_toolbar</argument>
                    </action>
                </block>
            </referenceContainer>
            <referenceBlock name="catalog.compare.sidebar" remove="true"/>
        </body>
    </page>

Template File

<?php
/**
* Product list template
*
* @var $block \Magento\Catalog\Block\Product\ListProduct
*/
?>
<?php
$block_obj = $block->getLayout()- 
>createBlock('Mymodule\BestsellerSlider\Block\Bestseller');
$_productCollection = $block_obj->getProductCollection();
?>
<div class="toolbar toolbar-products bestseller">
    <?php echo $block->getToolbarHtml() ?>
    <?php echo $block_obj->getPagerHtml(); ?>
</div>

....

<div class="toolbar toolbar-products bestseller-bottom">
    <?php echo $block_obj->getPagerHtml(); ?>
</div>
  • Can you update the codes which you have done so that I can help you? – Sukumar Gorai Dec 31 '19 at 8:04
  • @SukumarGorai I have updated the question with my files. Can you please check? – Keerthana Dec 31 '19 at 9:04
  • What do you mean by custom pagination? I am seeing that you are using magento pagination currently. – Sukumar Gorai Dec 31 '19 at 9:08
  • I just mean that, in my default listing page, i have called default Magento pagination. But in my custom module, i am using custom collection for pagination.. Can you please suggest me how can I call this pagination in default toolbar?? – Keerthana Dec 31 '19 at 9:12
  • I am not able to see your custom pagination? Where it is? Do you mean mymodule.bestsellerslider.pager? – Sukumar Gorai Dec 31 '19 at 9:14

Your Answer

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

Browse other questions tagged or ask your own question.