3

I'm trying to set the Magento 2 pager on my custom module, my custom module get the product collection and makes some special filters on the collection. I'm able to filter my products, but when I try to add the pager I get this error:

Exception #0 (Magento\Framework\Exception\LocalizedException): Element with ID 'pairings.product.pager' already exists.

Here my block:

<?php namespace <Vendor>\<Module>\Block; class Filter extends \<Vendor>\<OtherCustomModule>\Block\ProductAbstract {

const ENTITY_TYPE = 'catalog_product';

/**
 * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
 */
protected $_productCollection;

/**
 * Filter constructor.
 * @param \Magento\Catalog\Block\Product\Context $context
 * @param \Magento\Framework\Pricing\Helper\Data $priceHelper
 * @param \Magento\Catalog\Model\CategoryFactory $modelCategory
 * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection
 * @param array $data
 */
public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\Pricing\Helper\Data $priceHelper,
    \Magento\Catalog\Model\CategoryFactory $modelCategory,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection,
    array $data = []
)
{
    parent::__construct(
        $context,
        $priceHelper,
        $modelCategory,
        $data
    );
    $this->_productCollection = $productCollection;
}

public function getFilteredProducts()
{
    $params = $this->getRequest()->getParams();
    $productsFiltered = array();
    if (count($params)) {

        $collection = $this->_productCollection->create();
        $productsFiltered = $collection->addAttributeToSelect('*');
        $pageSize = 8;
        if (isset($params['limit'])) {
            $pageSize = $params['limit'];
            unset($params['limit']);
        }
        $page = 1;
        if (isset($params['page'])) {
            $page = $params['page'];
            unset($params['page']);
        }
        $ordersParam = array(
            'attribute' => 'name',
            'sorter' => 'asc'
        );
        if (isset($params['sort'])) {
            if ($params['sort'] == 'price_asc') {
                $ordersParam = array(
                    'attribute' => 'price',
                    'sorter' => 'asc'
                );
            } elseif ($params['sort'] == 'price_desc') {
                $ordersParam = array(
                    'attribute' => 'price',
                    'sorter' => 'desc'
                );
            }
            unset($params['sort']);
        }
        foreach ($params as $key => $param) {
            if ($key == 'prezzo') {
                $parts = explode(',', $param);
                $min = (isset($parts[0]) ? $parts[0] : 0);
                $max = (isset($parts[1]) ? $parts[1] : 400);
                $productsFiltered = $collection->addAttributeToFilter('price', array('gteq' => $min))
                    ->addAttributeToFilter('price', array('lteq' => $max));
            } else {
                $productsFiltered = $collection->addAttributeToFilter($key, array('in' => $param));
            }
        }
        $productsFiltered->setOrder($ordersParam['attribute'], $ordersParam['sorter']);
        $productsFiltered->setPageSize($pageSize);
        $productsFiltered->setCurPage($page);
    }

    return $productsFiltered;
}

protected function _prepareLayout()
{
    $this->pageConfig->getTitle()->set(__('Abbinamenti'));

    if ($this->getFilteredProducts()) {
        $pager = $this->getLayout()->createBlock(
            'Magento\Theme\Block\Html\Pager',
            'pairings.product.pager'
        )->setAvailableLimit(array(8=>8,16=>16,24=>24,32=>32,40=>40))->setShowPerPage(true)->setCollection(
            $this->getFilteredProducts()
        );
        $this->setChild('pager', $pager);
        $this->getFilteredProducts()->load();
    }

    return $this;
}

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

As you can see my custom module extend an other custom module that extends \Magento\Catalog\Block\Product\AbstractProduct.

In my .phtml I use getPagerHtml() method to get the pager.

EDIT

I followed this old post to create my paginator, but It doesn't work: Old post

  • Can any one tell what exactly this error means? – Mukesh Apr 23 '17 at 12:09
2

I had the same issue, I fixed it adding an if condition:

protected function _prepareLayout()
{
   parent::_prepareLayout();
   $this->pageConfig->getTitle()->set(__('Concept Sketch Requests'));

   if ($this->getRequestsCollection()) {
      $blockName = 'coneceptrequest.pager';
      $pager = null;
      if ($this->getLayout()->getBlock($blockName)) {
          $pager = $this->getLayout()->getBlock($blockName)
            ->setAvailableLimit(array(5=>5, 10=>10, 15=>15, 20=>20))
            ->setShowPerPage(true)->setCollection(
              $this->getRequestsCollection()
            );
      } else {
          $pager = $this->getLayout()->createBlock(
            'Magento\Theme\Block\Html\Pager',
            $blockName
          )->setAvailableLimit(array(5=>5, 10=>10, 15=>15, 20=>20))
          ->setShowPerPage(true)->setCollection(
            $this->getRequestsCollection()
          );
      }
      $this->setChild('pager', $pager);
      $this->getRequestsCollection()->load();
   }
   return $this;
}
0

Do getBlock instead of createBlock

    protected function _prepareLayout()
    {
        $this->pageConfig->getTitle()->set(__('Abbinamenti'));

        if ($this->getFilteredProducts()) {
            $pager = $this->getLayout()
                      ->getBlock('pairings.product.pager')
                      ->setAvailableLimit(array(8=>8,16=>16,24=>24,32=>32,40=>40))
->setShowPerPage(true)->setCollection(
                $this->getFilteredProducts()
            );
            $this->setChild('pager', $pager);
            $this->getFilteredProducts()->load();
        }
  • I get HTTP ERROR 500 in this way. – 3tech - Alessandro Staffolani Dec 6 '16 at 14:45
  • What is the error on the error log? – Goku Dec 6 '16 at 14:52
  • cache_invalidate: "invalidateInfo":{"tags":["interception","CONFIG"],"mode":"matchingTag"},"is_exception":false – 3tech - Alessandro Staffolani Dec 6 '16 at 14:54
  • What do you mean with load layout? I have run cache:flush command – 3tech - Alessandro Staffolani Dec 6 '16 at 14:58
  • That is not the php error, look inside you php error log – Goku Dec 6 '16 at 15:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.