Abilash Erikson
Abilash Erikson

Reputation: 351

Pagination in Magento widget block

Hi is there any way to do pagination in widget block . For example have a category with id 355 . I want to display that category product in a page . So i am using widget( following code )

{{widget type="Magento\CatalogWidget\Block\Product\ProductsList" show_pager="0" products_count="160" template="Magento_CatalogWidget::product/widget/content/grid.phtml" conditions_encoded="^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`category_ids`,`operator`:`==`,`value`:`355`^]^]"}}

. But there are upto 155 product in that category the product are displaying in the page . But for 155 product the page load time is too high . So if there will be pagination for that then its easy to load the products .

Upvotes: 2

Views: 1189

Answers (1)

Mohamed23gharbi
Mohamed23gharbi

Reputation: 1780

In fact Yes, pager is implemented for Magento\CatalogWidget\Block\Product\ProductsList, you just need to activate it using show_pager="1" and define how much products to show per page products_per_page="6" (if you ignore this param then default value is 5)

UPDATE : I guess you need to add the param page_var_name="np" where 'np' is the name of the pagination parameter (you can name it at your convenience), like following and this should resolve the pagination issue :

Your code shoud be like this :

{{widget type="Magento\CatalogWidget\Block\Product\ProductsList" show_pager="1" products_per_page="6" products_count="160" page_var_name="np" template="Magento_CatalogWidget::product/widget/content/grid.phtml" conditions_encoded="^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`category_ids`,`operator`:`==`,`value`:`355`^]^]"}}

@see : vendor/magento/module-catalog-widget/Block/Product/ProductsList.php

class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implements BlockInterface, IdentityInterface
{
    /**
     * Default value for products count that will be shown
     */
    const DEFAULT_PRODUCTS_COUNT = 10;

    /**
     * Name of request parameter for page number value
     *
     * @deprecated
     */
    const PAGE_VAR_NAME = 'np';

    /**
     * Default value for products per page
     */
    const DEFAULT_PRODUCTS_PER_PAGE = 5;

    /**
     * Default value whether show pager or not
     */
    const DEFAULT_SHOW_PAGER = false;
...


   /**
     * Retrieve how many products should be displayed
     *
     * @return int
     */
    public function getProductsPerPage()
    {
        if (!$this->hasData('products_per_page')) {
            $this->setData('products_per_page', self::DEFAULT_PRODUCTS_PER_PAGE);
        }
        return $this->getData('products_per_page');
    }

    /**
     * Return flag whether pager need to be shown or not
     *
     * @return bool
     */
    public function showPager()
    {
        if (!$this->hasData('show_pager')) {
            $this->setData('show_pager', self::DEFAULT_SHOW_PAGER);
        }
        return (bool)$this->getData('show_pager');
    }

    /**
     * Retrieve how many products should be displayed on page
     *
     * @return int
     */
    protected function getPageSize()
    {
        return $this->showPager() ? $this->getProductsPerPage() : $this->getProductsCount();
    }

Upvotes: 1

Related Questions