Reputation: 329
I am trying to limit the number of products from a certain category to 4 on my homepage.
The code I am trying to do this with is:
{{block type="catalog/product_list" column_count="4" category_id="13" template="catalog/product/list.phtml"}}
Here are some of the things I have tried:
num_products="4"
limit = 4, limit="4"
count = 4, count="4"
_productCollection="4"
_productsCount="4"
I have made a copy of list.phtml thinking there might be a way to change it in there, but was unable to find out a way.
At the very top pf list.phtml is this code:
<?php
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
?>
And under grid view there is this:
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
Any ideas on to limit the products either within the block or the template file?
Upvotes: 4
Views: 20335
Reputation: 21
IN Magento ver. 1.9.0.1 i found a simple solution just add these line in list.phtml just find the foreach loop it comes two times so need to add in both location.
<?php $i=0; ?>
<?php foreach ($_productCollection as $_product):
if($i == 6) break;
$i++;
?>
i put it for 6 record you may change it to as you required.
thanks
Upvotes: 2
Reputation: 1221
try this:
$_productCollection=$this->getLoadedProductCollection();
$_productCollection->getSelect()->limit(5);
Upvotes: 1
Reputation: 23
When I was facing this problem them i search to many sites but there is very less sites that make me understand of it.. I edited it myself by doing these step to show FIXED NUMBER OF PRODUCTS FROM CERTAIN CATEGORIES follows as:- go to
1) app\design\frontend\default\<your theme>\template\catalog\product
copy list.phtml and save as list_new.phtml
Now open list_new.phtml and search '' after ending the if loop insert this code
<?php if($i<=4): // for 4 product?>
and close after the list ended.
<?php endif // for 4 product?>
The code will look like this-
<?php if ($i++%$_columnCount==0): ?>
<ul class="products-grid">
<?php endif ?>
<?php if($i<=4): // for 4 product?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?>">
// some code here
</li>
<?php endif // for 4 product?>
2) Now go to CMS>Pages>select home page >content> and copy this code (change your categy id here)
{{block type="catalog/product_list" name="product_list" category_id="<category id>" column_count="4"mode="grid" template="catalog/product/list_new.phtml"}}
Upvotes: 1
Reputation: 17656
Try
{{block type="catalog/product_list" limit="4" category_id="13" template="catalog/product/list.phtml"}}
See
Upvotes: 3
Reputation: 2384
The quicker is replace column_count=4
by is_homepage=1
and in the phtml add this :
<?php if($this->getIsHomepage() && $i==4) break; ?>
before this :
<?php if ($i++%$_columnCount==0): ?>
Then you will have just 1 row on homepage (if 4 by row as I suppose) so total 4 products
Upvotes: 5