Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Featured products module - how do I enforce a limit to the number of product displayed? 
 
cclohman
Sr. Member
 
Avatar
Total Posts:  82
Joined:  2008-06-16
Los Angeles
 

Here’s my problem - I need to show the 100 most recent products on my client’s site.  I’ve created a module that overrides the product list and changes the behavior for certain categories.  The strange thing is, despite using the setPageSize() directive on my products collection, it’s still returning the entire product catalog (1500 some products).  It’s the toolbar that seems to be showing the wrong number, but I don’t understand how or why that would be using a different products collection that the one I’m overriding?  Here’s my code:

class Tridian_Newarrivals_Block_Newarrivals extends Mage_Catalog_Block_Product_List
{
    
     
public $_collection;  
    
     protected function 
_getProductCollection()  
     
{  
        $curr_category 
Mage::registry('current_category')->getName();
        
$curr_category strtolower($curr_category);
        if(
$curr_category == 'new arrivals')
        
{
            $storeId    
Mage::app()->getStore()->getId();  
            
$product    Mage::getModel('catalog/product');  
            
            
$products $product->setStoreId($storeId)  
                ->
getCollection()  
                ->
addAttributeToSelect(array('name''price''small_image'), 'inner')
                ->
addAttributeToSelect(array('special_price''special_from_date''special_to_date'), 'left')
                ->
setOrder('created_at''desc')
                ->
setPageSize(12)
                ;  
            
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
            
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
            
            
$this->_collection $products;  
        
}
        
else
        
{
            $this
->_collection parent::_getProductCollection();
        
}
        
        
return $this->_collection;  
    
     
}  
}

You can see here what the end result of my efforts are.  So close.  Please help.

Thanks,
Chris

 
Magento Community Magento Community
Magento Community
Magento Community
 
cclohman
Sr. Member
 
Avatar
Total Posts:  82
Joined:  2008-06-16
Los Angeles
 

Okay, I figured it out.  Turns out the class variable should have been named $_productCollection NOT $_collection, in order to override the parent class value.  I also added in some logic to pull parent products where applicable.  Thanks to Luxe Soft’s Bestseller extension for that addition.  Here’s the final version of the class for reference.

class Tridian_Newarrivals_Block_Newarrivals extends Mage_Catalog_Block_Product_List
{
    
    
//public $_collection;  
    
    
public function getProductsLimit() 
    

        
if ($this->getData('limit')) {
            
return intval($this->getData('limit'));
        
else {
            
return 100;
            
//return $this->getToolbarBlock()->getLimit(); 
        
}
    }

     
protected function _getProductCollection()  
     
{  
        
if (is_null($this->_productCollection)) {
            $curr_category 
Mage::registry('current_category')->getName();
            
$curr_categoryid Mage::registry('current_category')->getId();
            
$curr_category strtolower($curr_category);
            if(
$curr_category == 'new arrivals')
            
{
                $storeId    
Mage::app()->getStore()->getId();  
                
$product    Mage::getModel('catalog/product');  
                
                 
$visibility = array(  
                     
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,  
                     
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG  
                 
);  
                
                
$this->_productCollection $product->setStoreId($storeId)  
                    ->
getCollection()  
                    ->
addAttributeToSelect(array('name''price''small_image'), 'inner')
                    ->
addAttributeToSelect(array('special_price'), 'left')
                    ->
setOrder('created_at''desc')
                    ->
setPageSize($this->getProductsLimit())
                    ->
addAttributeToFilter('visibility'$visibility)
                    ->
addUrlRewrite($curr_categoryid);
                    ;
//->load(true);  
                //Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);
                //Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_productCollection);
                
                
$checkedProducts = new Varien_Data_Collection();
                
$curPage 1;
                while (
count($checkedProducts) < $this->getProductsLimit()) {
                    $this
->_productCollection->clear()->setCurPage($curPage)->load();
                
                    if (
$this->_productCollection->getCurPage() != $curPage{
                        
break; //if bestsellers list is over simply exit
                    
}
                    
foreach ($this->_productCollection as $k => $p{
                        $p 
$p->loadParentProductIds();
                        
$parentIds $p->getData('parent_product_ids');
                        
// if product is part of configurable product get first parent product
                        
if (is_array($parentIds) && !empty($parentIds)) {
                            
if (!$checkedProducts->getItemById($parentIds[0])) {
                                $parentProduct 
Mage::getModel('catalog/product')->setStoreId($storeId)->load($parentIds[0]);
                                if (
$parentProduct->isVisibleInCatalog()) {
                                    $checkedProducts
->addItem($parentProduct);
                                
}
                            }
                        } 
else {
                            
if (!$checkedProducts->getItemById($k)) {
                                $checkedProducts
->addItem($p);
                            
}
                        }
                        
if (count($checkedProducts) >= $this->getProductsLimit()) {
                            
break;
                        
}
                    }
                    $curPage
++;
                
}
                $this
->_productCollection $checkedProducts;
            
}
            
else
            
{
                $this
->_productCollection parent::_getProductCollection();
            
}
        }
        
return $this->_productCollection;  
    
     
}  
}

I’m still having problems getting the toolbar to show up.  As it stands, all 100 newest products display (which isn’t so bad).  Would love to get that to be an option for display.

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  901
Joined:  2008-10-21
 

I’m interested to see whay you’ve done here.

I currently have a display random product from the side and I think the new.phtml file is annoying because you have to setup products to turn on and off.

How did you get on with displaying your products by ID?

lol, just checked out your site. Just realised I’ve been on this site once already.

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  901
Joined:  2008-10-21
 

Hmm, i’m trying this out, but it just doesnt seem to be restricting the number of products and it is only picking up products from within the root category (which is none) and none of the others.

thanks for the tip smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  901
Joined:  2008-10-21
 

#Update,

I have used this code successfully to display a list of 5 products. Code found at below link.

http://www.magentocommerce.com/boards/viewthread/1243/

Thanks to cclohman and aheadWorks for helping me out with this one.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Rinku
Member
 
Total Posts:  41
Joined:  2008-09-17
New Delhi
 

Hello All,

I have a problem, I have approx 1500 records in product collection. But I want only 100 products to show. Kindly let me know how can I set limit in product collection.

here is my code.

$this->_productCollection = Mage::getResourceModel(’reports/product_collection’)
->setStoreId($storeId)
->addAttributeToFilter(’visibility’,$visibility)
->addAttributeToSelect(’*’)
->addViewsCount()
->joinAttribute(’status’, ‘catalog_product/status’, ‘entity_id’, null, ‘inner’)
->addAttributeToFilter(’status’,1);

Thank you.
Sandeep

 
Magento Community Magento Community
Magento Community
Magento Community
 
cclohman
Sr. Member
 
Avatar
Total Posts:  82
Joined:  2008-06-16
Los Angeles
 

Rinku,

You need to override the getProductsLimit() function in Mage_Catalog_Block_Product_List as shown in my Newarrivals code higher up on this page.  In order to get the toolbar to display the correct number, I found that overriding getSelectCountSql() to be effective.  Code is below to limit the toolbar to 108 items total.

class Tridian_Newarrivals_Model_Newarrivals extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
{
    
/**
     * Render SQL for retrieve product count
     *
     * @return string
     */
    
public function getSelectCountSql()
    
{
        $curr_category 
Mage::registry('current_category')->getName();
        
$curr_category strtolower($curr_category);
        if(
$curr_category == 'new arrivals')
        
{
            $sql 
"select 108";
        
}
        
else
        
{
            $sql 
parent::getSelectCountSql();
        
}
        
return $sql;
    
}

}

Good luck.
Chris

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top