4

I am using Magento 1.7.0.2 with shopper theme. I want to limit the number of products to 4 for each category displayed on the homepage. I have duplicated list.phtml and created three different list files for categories.

Code on homepage static CMS block is:

{{block type="catalog/product_list" category_id="116" template="catalog/product/featured_list.phtml" num_products="4" columnCount="4"}}
{{block type="catalog/product_list" category_id="104" template="catalog/product/jewellery_list.phtml" num_products="4" columnCount="4"}}
{{block type="catalog/product_list" category_id="117" template="catalog/product/bags_list.phtml" num_products="4" columnCount="4"}}

But this does not work. I am still getting 9 products instead of 4 for all the categories. 9 is the default number of products for grid view set in magento admin. If I change that 9 to 4, then the homepage shows 4 products each but this also changes the number of products shown for each category page which I do not want.

I have looked at many solutions offered but somehow there appears to be no talk about limiting number of products displayed in a custom list or grid.

Any help and guidance will be appreciated....

  • 4
    I'm voting to close this question as off-topic because too old without an accepted answer – SR_Magento Jun 14 '15 at 13:10
8

I have run into the same issue during a site I am managing. The proposed solutions do not appeal to me, as they require creating blocks to extend the core product list class, or as proposed to manipulate the collection directly in a template.

I wanted a cleaner way, and found one.

Since we are not interested in a linkfest, I re-created (most) of my blog post here as an answer.

The first step was to determine how magento internals get to the point of having the product collection available to the core template '/app/design/frontend/base/default/template/catalog/product/list.phtml' in the first call of '$_productCollection=$this->getLoadedProductCollection();'

Thus, using some breakpoints, and stepping back through the code form the point in the template call, you will find the following code flow path:

Mage_Catalog_Block_Product_List::_beforeToHtml()

enter image description here

Here you can see that internals instantiates a toolbar object. The method Mage_Catalog_Block_Product_List::getToolbarBlock() looks as such:

enter image description here

So, internals first checks to see if there is not already a toolbar present, by using the name of the toolbar stored in the variable ToolBarBlockName, and if not, it creates the required block. This is interesting, and vital to the solution.

Moving on through the code flow in the _beforeToHtml() method (see pointer #2), I find that the collection is set to the toolbar.

Stepping into that code, you find the following:

enter image description here

And digging in just a little further, the ->getLimit() method reveals this code:

enter image description here

Thus, if the toolbar has a value set to _current_limit, internals will use that as the limit on the collection, and ignore the default (or lack of defau7lt) limits set to the toolbar object.

Thus it is very possible to inject a limit to the collection as it is built.

The basic steps would be :

  1. Load the toolbar object
  2. Store the created toolbar name in the required variable, so it can be reused later
  3. Set the _current_limit in the created toolbar

The best place (please correct me if there is a better place/event) is via the event 'core_block_abstract_prepare_layout_before'.

This will allow me to instantiate the toolbar, save it, and inject my limit information.

The code to do this was easy, and straight forward.

/**
 *  Observer to manipulate the layout of blocks
 */
class ProxiBlue_CatalogListFilters_Model_Observer {

    public function core_block_abstract_prepare_layout_before(Varien_Event_Observer $observer) {
        try {
            $block = $observer->getEvent()->getBlock();
            if($block instanceof Mage_Catalog_Block_Product_List) {
                if($block->getProductLimit()){
                    $toolbar = $block->getToolbarBlock();
                    $block->setToolbarBlockName($toolbar->getNameInLayout());
                    $toolbar->setData('_current_limit', $block->getProductLimit());
                }    
            }
        } catch (Exception $e) {
            mage::logException($e);
        }
        return $this;
    }

}

The CMS block used would look like this:

{{block type="catalog/product_list" category_id="4" product_limit="5" template="catalog/product/list.phtml"}}

I have packaged this solution as a module available via github: https://github.com/ProxiBlue/CatalogListFilters

| improve this answer | |
4

Just ran into this problem myself. I wanted to display multiple selected categories on a cms page (in this case home page) with a limit of 4 products using the grid mode to have the products appear horizontally across the page.

Open your the copy of your modified list.phtml file and find this line:

$_productCollection=$this->getLoadedProductCollection();

Add this directly bellow:

 $_productCollection->clear()
                    ->setPageSize(4)
                    ->load();

Note you can change the #4 to the amount of products you would like to limit.

| improve this answer | |
1

Instead of new_products="4" try products_count="4".

| improve this answer | |
  • products_count is also not working!! – Puneet Nov 18 '13 at 6:50
1

The products_count option is only available for the block type catalog/product_new but that block will limit the products shown by a data range.

If you want this to work for the normal list you could create your own block that extends the list and add the products count code into this block.

A good example of this can be found here note that they use limit instead of products count. Or you could simply add the functions setProductsCount and getProductsCount plus constants to your new block and update the _getProductCollection to use this value.

| improve this answer | |

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.