8

Problem : Trying to override the default Magento phtml file, im getting error

I need to get these: (Pagination in footer, Sorter)

enter image description here

My custom Modules:

1) Vendor/TglsSearch  --> Override from Magento CatalogSearch
2) Vendor/Tglcatalog ---> from Magento Catalo

In app\design\frontend\Vendor\tag

enter image description here

UPDATED CODE:

REMOVED the below folder

In Magento_Catalog folder :

templates\product\list\toolbar.phtml
templates\product\list\toolbar\amount.phtml
templates\product\list\toolbar\limiter.phtml
templates\product\list\toolbar\sorter.phtml
templates\product\list\toolbar\viewmode.phtml

This is what i get blank page without results in front page:

enter image description here

Added the mentioned code in Block file:

Vendor\Module\Block\Product\ListProduct.php

class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
{
    public function __construct(
    /*passing all Constructors parameters to the parent class */
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
    \Magento\Catalog\Model\Layer\Resolver $layerResolver,
     CategoryRepositoryInterface $categoryRepository,
    \Magento\Framework\Url\Helper\Data $urlHelper,

    \Vendor\Module\Helper\Data $tglssearchHelper,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $catalogResourceModelProductCollectionFactory,
    \Magento\Catalog\Model\Config $catalogConfig,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Catalog\Model\ProductFactory $productFactory,      //Pagination
    array $data = []
) {
    $this->tglssearchHelper = $tglssearchHelper;
    $this->catalogResourceModelProductCollectionFactory = $catalogResourceModelProductCollectionFactory;
    $this->catalogConfig = $catalogConfig;
    $this->storeManager = $storeManager;

    $this->_productFactory = $productFactory;  //Pagination
    $this->urlHelper = $urlHelper;

      parent::__construct(
        $context,
        $postDataHelper,
        $layerResolver,
        $categoryRepository,
        $urlHelper,
        $data
    );
    //updated code
     $collection= $this->_getProductCollection();
    $this->setCollection($collection);

}

public function _prepareLayout(){
   parent::_prepareLayout();
    if ($this->_getProductCollection()) { 

        // create pager block for collection 

        $toolbar = $this->getToolbarBlock();  

        $pager = $this->getLayout()->createBlock(
                        'Magento\Theme\Block\Html\Pager', 'list.pager'
                )->setCollection(
                $this->_getProductCollection() // assign collection to pager
        );  
        $toolbar->setChild('product_list_toolbar_pager', $pager); // set pager block in layout
        // 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);
        }
        $toolbar->setCollection($collection);

        $this->setChild('toolbar', $toolbar);
        $this->_getProductCollection()->load();
   }
    return $this;
}

protected function _getProductCollection()
{
      $tagalys = $this->tglssearchHelper->getSearchData();

        if($tagalys == false) {
            return parent::_getProductCollection();
        } else {

        $searchResult = $tagalys;

        if(empty($searchResult)) {
            return parent::_getProductCollection();
        }

        $collection = $this->_productCollection = $this->catalogResourceModelProductCollectionFactory->create()
             ->addAttributeToSelect($this->catalogConfig->getProductAttributes())
             ->setStore($this->storeManager->getStore())
             ->addFieldToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
             ->addAttributeToFilter( 'entity_id', array( 'in' => $searchResult['results'] ) );

        $orderString = array('CASE e.entity_id');
        foreach($searchResult['results'] as $i => $productId) {
            $orderString[] = 'WHEN '.$productId.' THEN '.$i;
        }
        $orderString[] = 'END';
        $orderString = implode(' ', $orderString);

    $collection->getSelect()->order(new \Zend_Db_Expr($orderString));

        return $this->_productCollection;

    }
}


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

public function getMode()                       //Pagination
{
    return $this->getChildBlock('toolbar')->getCurrentMode();
}

public function getToolbarHtml()        //Pagination
{
    return $this->getChildHtml('toolbar');
}

protected function getPriceRender()
{
    return $this->getLayout()->getBlock('product.price.render.default');
}

protected function _getConfig()
{
    return $this->_catalogConfig;
}
}

For phtml:

\app\design\frontend\Vendor\tag\Magento_Catalog\templates\product\list.phtml

<?php
   $_productCollection = $block->getLoadedProductCollection();
   $_helper = $this->helper('Magento\Catalog\Helper\Output');
?>
 <?php if (!$_productCollection->count()): ?>
<div class="message info empty"><div><?php /* @escapeNotVerified */ echo __('We can\'t find products matching the selection.') ?></div></div>
 <?php else: ?>
   <?php echo $block->getToolbarHtml() ?>        //Have called here
     ....
     ....
    <?php echo($iterator == count($_productCollection)+1) ? '</li>' : '' ?>
        <?php endforeach; ?>
    </ol>
  </div>
  <?php echo $block->getToolbarHtml() ?>    //Have called here
    ....

This is what i get if i click on Category link in frontend.

enter image description here

In Magento_CatalogSearch :

templates\result.phtml

In Magento_Theme :

templates\html\pager.phtml
layout\default.html
web\images\my_logo.png

NOW the result.phtml error seems to be gone, but still my products are not rendering in frontend with pagination

In

Admin->Content->Design configuration-> Enabled my Custom Theme.

NOTE: In my custom module, i have removed the view folder which had templates and layout files.

Problem:

I have to get the pager(pagination), Sorter etc from the above phtml files.

1) But my products are not rendered from result.phtml (But my var/log has product ids)... frontend not displaying

2) No pagination or sorter appearing

2
  • refer this answer to solve your 1st and 3rd problem magento.stackexchange.com/a/86190/36463 . In this answer link follow "Mage2.PRO" answer. kandy ans not work for me Commented Aug 26, 2016 at 11:31
  • From the above link, should i have to give as mentioned by Mage2.pro in the app/desin/<overidden template file> ?if so i tried the same, but Pagination is not appearing(i have kept pager phtml and toolbar.phtml) and catalog page is coming like as shown in the screenshot above...Pls suggest
    – Sushivam
    Commented Aug 28, 2016 at 17:22

1 Answer 1

5
+25

It seems you have to set pager and sorting in your custom phtml file.

I have followed below steps pr add pagination and sorting.

In your block file add below code for add pagination:

public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Catalog\Model\ProductFactory $productFactory,
    \Magento\Framework\Url\Helper\Data $urlHelper,
    array $data = []
) {
    $this->_productFactory = $productFactory;
    $this->urlHelper = $urlHelper;
    parent::__construct($context, $data);

    // Get your custom collection here

    $collection = $this->getCustomCollection();

    $this->setCollection($collection);
}

public function _prepareLayout()
{

    parent::_prepareLayout();
    if ($this->getCollection()) {

        // create pager block for collection 

        $toolbar = $this->getToolbarBlock();

        $pager = $this->getLayout()->createBlock(
            'Magento\Theme\Block\Html\Pager', 'list.pager'
        )->setCollection(
            $this->getCollection() // assign collection to pager
        );
        $toolbar->setChild('product_list_toolbar_pager', $pager); // set pager block in layout
        // called prepare sortable parameters
        $collection = $this->getCollection();

        // 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);
        }
        $toolbar->setCollection($collection);

        $this->setChild('toolbar', $toolbar);
        $this->getCollection()->load();
    }

    return $this;
}

public function getToolbarHtml()
{
    return $this->getChildHtml('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;
}

public function getMode()
{
    return $this->getChildBlock('toolbar')->getCurrentMode();
}

Also define below property:

protected $_defaultToolbarBlock = 'Magento\Catalog\Block\Product\ProductList\Toolbar';

In phtml file add below code before and after listing:

<?php echo $block->getToolbarHtml() ?>
16
  • The phtml files are same as in core pasted in the above mentioned folder! and i have added the protected $_defaultToolbarBlock= 'Magento\Catalog\Block\Product\ProductList\Toolbar'; in my custom ListProduct.php
    – Sushivam
    Commented Aug 30, 2016 at 6:26
  • All my custom phtml files seems to be called, but y not my categories and product collection in search not loading!
    – Sushivam
    Commented Aug 30, 2016 at 7:22
  • for pagination you don't need to overwrite any phtml file. and also it seems you have not set collection in your construct. and i am not sure but try your toolbar code in _prepareLayout function instead of _beforeToHtml Commented Aug 30, 2016 at 9:26
  • Can u please give me an example code to get the pagination (without overriding phtml) in my block file? i mean i need what are the exact functions that i have to keep, im confused!
    – Sushivam
    Commented Aug 30, 2016 at 9:31
  • I commented out -beforeToHtml() and added the getToolbarBlock() in prepareLayout() an also set the collection in construct(), i get main.CRITICAL: Illegal state [] [] in system.log
    – Sushivam
    Commented Aug 30, 2016 at 9:39

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.