Showing More Than Default Featured Products on Magento Homepage
If you’re trying to display more than the default number of featured products (currently set to 5) on your Magento homepage without making core code changes, follow these steps:
Step 1: Understand the Current Setup
You’ve already done a great job setting up a template file (featured-products.phtml) and calling it in a static block. This approach allows you to control the product display logic directly.
Your current setup is using a loop to iterate through the product collection based on the number of columns you define ($_columnCount). However, the limit seems to be hardcoded or constrained by Magento’s core functionality.
Step 2: Change the Product Collection Logic
In your featured-products.phtml file, you’re currently getting the product collection using $this->getProductCollection(). This method likely retrieves products based on the ‘new’ flag and applies a limit of 5 items. To show more than this default count, you need to modify how the product collection is retrieved.
Step 3: Create a New Product Collection
Instead of using getProductCollection(), create a new product collection object directly:
<?php
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect(array('name', 'sku', 'price', 'image'))
->setVisibility(array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH))
->addFieldToFilter('new_from_date', array('timestamp' => true, 'from' => $now, 'to' => now()))
->setPage(1, 20); // Change the second parameter to set your desired number of products per page
?>
This code creates a new product collection that filters for new products with a visibility setting of both (invisible and visible). It selects necessary attributes and sets the pagination parameters.
Step 4: Update Your Loop
Replace the existing loop in featured-products.phtml with this modified version:
<?php if ($collection->getSize()): ?>
<?php $i = 0; foreach ($collection as $_product): ?>
// Rest of your code...
<?php endforeach; ?>
<?php endif; ?>
Step 5: Update Your Static Block
Make sure your static block is set to display on the homepage and that the template file (catalog/featured-products.phtml) is correctly referenced.
Conclusion By creating a new product collection object with custom filters and pagination, you can effectively override the default limit of featured products displayed on the Magento homepage. This approach allows you to maintain flexibility in your product display logic without requiring core code changes or database modifications.
Remember to test this solution thoroughly, as changing the number of displayed items may impact your site’s performance and layout. Always refer to the official Magento documentation for best practices and compatibility considerations with your specific version (1.7).
For further guidance on customizing Magento templates and blocks, consider exploring resources available at Laravel Company, which offers expert insights into Magento development strategies.
Happy coding!