0

I am adding pagination block to my custom shop by brand module, here is Block file

<?php
namespace PinkBlue\ShopByBrand\Block;
use Magento\Catalog\Model\Product\Attribute\Source\Status;

class View extends \Magento\Framework\View\Element\Template
{
    protected $_defaultToolbarBlock = 'Magento\Catalog\Block\Product\ProductList\Toolbar';
    /**
     * @var \Magento\Framework\App\Http\Context
     */
    protected $httpContext;
    /**
     * Catalog product visibility
     *
     * @var \Magento\Catalog\Model\Product\Visibility
     */
    protected $_catalogProductVisibility;

    /**
     * Product collection factory
     *
     * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
     */
    protected $_productCollectionFactory;
    protected $_productCollection;
    /**
     * Image helper
     *
     * @var Magento\Catalog\Helper\Image
     */
    protected $_imageHelper;
     /**
     * @var \Magento\Checkout\Helper\Cart
     */
    protected $_cartHelper;

    protected $_brandFactory;


    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
        \Magento\Framework\App\Http\Context $httpContext,
        \PinkBlue\ShopByBrand\Model\BrandFactory $brandFactory,
        array $data = []
    ) {
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->_catalogProductVisibility = $catalogProductVisibility;
        $this->httpContext = $httpContext;
        $this->_imageHelper = $context->getImageHelper();
        $this->_brandFactory = $brandFactory;
        $this->_cartHelper = $context->getCartHelper();
        parent::__construct(
            $context,
            $data
        );
    $this->setCollection($this->getProductCollection());
    }
     public function getAddToCartUrl($product, $additional = [])
    {
            return $this->_cartHelper->getAddUrl($product, $additional);
    }



    public function _prepareLayout()
    {
        parent::_prepareLayout();
        $brand = $this->getBrand();
        $brand_url = ($brand->getUrlKey())?
            $this->getUrl('brand').$brand->getUrlKey():
            $this->getUrl('brand/view/',array('id'=>$brand->getId()));
        $this->pageConfig->addRemotePageAsset(
            $brand_url,
            'canonical',
            ['attributes' => ['rel' => 'canonical']]
        );
        //sup code for pagination
        if ($this->getCollection()) {
            $pager = $this->getLayout()->createBlock(
                'Magento\Theme\Block\Html\Pager',
                'reward.history.pager');
            $pager->setLimit(36)
            ->setShowAmounts(false)
            ->setCollection($this->getCollection());
        $this->setChild('pager', $pager);
        $this->getCollection()->load();
        }
        //Sup code ends
        return $this;
    }
    /**
     * @return string
     */
    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }

    public function getBrand(){
        $id = $this->getRequest()->getParam('id');
        if ($id) {
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $model = $objectManager->create('PinkBlue\ShopByBrand\Model\Items');
            $model->load($id);
            return $model;
        }
        return false;
    }

    public function getProductCollection()
    {
        if($this->_productCollection===NULL) {
            $brand = $this->getBrand();
            $collection = $this->_productCollectionFactory->create();
            $collection->addAttributeToSelect('*');
            $collection->addAttributeToSelect('name');
            $collection->addStoreFilter()->addAttributeToFilter('manufacturer', $brand->getAttributeId());

            $collection->addAttributeToFilter('status', Status::STATUS_ENABLED);
            $collection->addAttributeToFilter('visibility', array('neq' => \Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE));
            $this->_productCollection = $collection;
        }
        return $this->_productCollection;
    }

    public function imageHelperObj(){
        return $this->_imageHelper;
    }

    public function getProductPricetoHtml(
        \Magento\Catalog\Model\Product $product,
        $priceType = null
    ) {
        $priceRender = $this->getLayout()->getBlock('product.price.render.default');
        $price = '';
        if ($priceRender) {
            $price = $priceRender->render(
                \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
                $product
            );
        }
        return $price;
    }

    /**
     * Retrieve list toolbar HTML
     *
     * @return string
     */
    public function getToolbarHtml()
    {
        return $this->getChildHtml('toolbar');
    }
    /**
     * Retrieve additional blocks html
     *
     * @return string
     */
    public function getAdditionalHtml()
    {
        return $this->getChildHtml('additional');
    }
    /**
     * Retrieve Toolbar block
     *
     * @return \Magento\Catalog\Block\Product\ProductList\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;
    }
    /**
     * Retrieve current view mode
     *
     * @return string
     */
    public function getMode()
    {
        return $this->getChildBlock('toolbar')->getCurrentMode();
    }
    /**
     * Need use as _prepareLayout - but problem in declaring collection from
     * another block (was problem with search result)
     * @return $this
     */
    protected function _beforeToHtml()
    {
        $toolbar = $this->getToolbarBlock();

        // called prepare sortable parameters
        $collection = $this->getProductCollection();

        // 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);
        }

        // set collection to toolbar and apply sort
        $toolbar->setCollection($collection);
        $this->setChild('toolbar', $toolbar);
        $this->getProductCollection()->load();
        return parent::_beforeToHtml();
    }
}

I want to make the limit dynamic but here i have set it as

 $pager->setLimit(36)

How Can I achieve this?

1 Answer 1

3
class Index extends \Magento\Framework\View\Element\Template {

    protected $context;
    protected $scopeConfig;

    public function __construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig) {
        $this->context = $context;
        $this->scopeConfig = $scopeConfig;
        parent::__construct($context);
    }

    /**
     * @return $this
     */
    protected function _prepareLayout() {

        parent::_prepareLayout();

        $limit = $this->context->getRequest()->getParam('limit');
        $limit = addslashes(strip_tags($limit));

        $pagingVal = $this->scopeConfig->getValue('<value path>', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);  // 36

        $pagingLimit = (isset($limit) && !empty($limit)) ? $limit : $pagingVal;

        ->setLimit($pagingLimit) // Change Variable in your query

        return $this;
   }
}
5
  • $pagingLimit = (isset($limit) && !empty($limit)) ? $limit : <Your Value>; I want this <your value > programatically taken from admin config. Jan 23, 2017 at 9:28
  • Check updated answer
    – Jackson
    Jan 23, 2017 at 9:31
  • How can I make it so that user can change from frontend - want to add block Show per page ? Jan 23, 2017 at 9:54
  • ->setAvailableLimit(array(5 => 5, 10 => 10, 15 => 15, 20 => 20, 25 => 25))->setShowPerPage(true)->setLimit(
    – Jackson
    Jan 23, 2017 at 9:55
  • my pagination is not showing at default position, which block i should render in template. Jan 23, 2017 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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