8

I'm trying to move the toolbar that shows the sorting options and the pager out of the product list so I can bundle it with the breadcrumbs and the category name.

I found this site that worked for older versions of Magento but it's not working for CE 1.7 or EE 1.12.

In the comments someone had some code to move it to the root but that also doesn't work. This was the code to move it to category/view which would help:

$toolbar = $this->getChild('product_list')->getToolbarBlock();
$toolbar->setCollection($this->getChild('product_list')->getLoadedProductCollection());
echo $toolbar->toHtml();

If there is another way to group the breadcrumbs, category name and toolbar together let me know.

4 Answers 4

13

Catalog/product_list and catalog/product_list_toolbar are so wired trough with each other that you're stuck with using them together. If you only want to display toolbar, though, you'll need to create an (almost) empty product_list template.

Say you want toolbar displayed in the breadcrumbs block, but this should work with any block really.

Put the following update somewhere in your layout/local.xml (you may need one for catalog_category_default, too):

<catalog_category_layered>
    <reference name="breadcrumbs">
        <block type="catalog/product_list" name="toolbar_only" template="catalog/product/list/toolbar_only.phtml">
            <block type="catalog/product_list_toolbar" name="product_list_toolbar_only" template="catalog/product/list/toolbar.phtml">
                <block type="page/html_pager" name="product_list_toolbar_pager"/> 
            </block>    
            <action method="setToolbarBlockName"><name>product_list_toolbar_only</name></action>
        </block>            
    </reference>            
</catalog_category_layered>

Create a one-liner product list template in template/catalog/product/list/toolbar_only.phtml:

<?php echo $this->getToolbarHtml() ?>

You can then insert the new block where ever you want in your breadcrumbs template:

template/page/html/breadcrumbs.phtml:

<?php echo $this->getChildHtml('toolbar_only'); ?>
4
  • This is a pretty good method too - I completely agree with keeping product lists and toolbars together at all costs. I would be interested in hearing your opinion on the javascript method I posted earlier. Mar 8, 2013 at 6:25
  • @Jan Tomka great answer! I was struggling with trying to push a block between the toolbar and the products themselves, and your solution made it possible. thanks!
    – Ronen Ness
    Dec 1, 2015 at 14:00
  • The problem with this answer is that it ends up triggering the configurable swatches observer to run again because the catalog/product_list block exists twice, and the catalog_block_product_list_collection method gets dispatched twice (which the observer is observing). This drastically increases category page load time.
    – scrowler
    Sep 26, 2016 at 2:15
  • True that. The original answer was written before the configurable swatches came around (not that I checked for multiple observer executions at that time). It might be best to use a separate block type extended from catalog/product_list, with the event dispatch removed from _beforeToHtml().
    – Jan Tomka
    Sep 27, 2016 at 1:49
4

After trying to move the toolbar block through layout XML and template hacks I decided it was a lot easier to move it via javascript. I think this way gives you more control without any risk of breaking functionality.

jQuery(document).ready(function($) {
     $(".breadcrumbs").after($(".category-products > .toolbar"));
});

Note this method only moves the first toolbar and not the bottom one using the specific selector .category-products > .toolbar

Some more reasoning as to why this method is best:

  1. The product list and toolbar are heavily intertwined, referencing each other in their blocks so it makes sense to keep them together in terms of code - they go hand in hand
  2. Less code and less changes to block logic means less probability for bugs to pop up
  3. Using javascript you can easily move it anywhere on the page
  4. Using layout XML you can only include the javascript on pages with a product list
  5. Moving via javascript is extendable and future-proofed - if the logic behind the product list or toolbar blocks changes in the new releases of Magento this method will still work.
1
  • I would totally go this method but I am trying to make the site as use as little javascript as possible even though I know Magento is javascript reliant.
    – kel
    Mar 8, 2013 at 15:33
4

LuFFy's code is not far off

To copy the block to other parts of your site, you can get the main layout object, and then just get the content block by name

      $layout = Mage::app()->getLayout();
      $content = $layout->getBlock('content');
      $categoryProducts = $content->getChild('category.products');
      $productList = $categoryProducts->getChild('product_list');
      $toolbar = $productList->getToolbarBlock();
      $toolbar->setCollection($productList->getLoadedProductCollection());
      echo $toolbar->toHtml();
2
  • Where to use this? Oct 12, 2015 at 11:28
  • @PratikCJoshi I put this code directly in my catalog/layer/view.phtml file and it worked perfectly, so you should be able to just place this in any template file where you want the toolbar to appear.
    – gregdev
    Sep 7, 2016 at 8:07
0

Try The Code :

getParentBlock();
$content = $parent->getChild(‘content’);
$categoryProducts = $content->getChild(‘category.products’);
$productList = $categoryProducts->getChild(‘product_list’);
$toolbar = $productList->getToolbarBlock();
$toolbar->setCollection($productList->getLoadedProductCollection());
echo $toolbar->toHtml();
2
  • That was the same code in the comments and it gives me this error: Call to undefined function getParentBlock()
    – kel
    Mar 7, 2013 at 20:42
  • Oh...!! Same i found on another site.
    – LuFFy
    Mar 8, 2013 at 12:50

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.