Changing Product Page Size for Specific Category in Magento
In Magento 1.x, you can set a default product page size across all categories from the admin panel. However, if you want to override this setting for a specific category, you’ll need to create a custom collection and handle pagination manually.
Let’s walk through how to achieve this.
Understanding the Default Behavior
By default, Magento allows you to set the number of products to display per page on the frontend. You can find this setting in System > Configuration > Catalog > Frontend > Products per Page on Category View.
Creating a Custom Collection for Your Category
First, you need to create a custom collection to fetch the products for your specific category.
$categoryId = 4; // replace with your category ID
$category = Mage::getModel('catalog/category')->load($categoryId);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($category);
In this example, we’re fetching the category object by its ID and then creating a new product collection. The addCategoryFilter method is used to filter the products belonging to this specific category.
Overriding the Default Page Size
To override the default page size for this specific collection, you can use the setPageSize method:
$collection->setPageSize(24); // set your desired number of products per page
In our case, we’re setting the page size to 24. This means that when this category is displayed on the frontend, it will show 24 products per page.
Handling Pagination
Magento handles pagination automatically for you if you use the addCategoryFilter method. However, since we’re creating a custom collection, we need to handle the pagination manually.
$collection->setCurPage(1); // set current page number
$pager = $this->getLayout()->createBlock('page/html_pager');
$pager->setCollection($collection);
In this code snippet:
- We’re setting the current page to 1.
- We’re creating a new object of
Mage_Page_Block_Html_Pagerblock, which is Magento’s default pager block. - Then we’re setting our custom collection as the collection for the pager.
Displaying the Products
Finally, you can display your products using the following code:
foreach ($collection as $product) {
echo $product->getName() . '<br>';
}
This will print out the name of each product in the collection on a new line.
Conclusion
In Magento 1.x, changing the page size for a specific category requires creating a custom collection and handling pagination manually. By following the steps outlined above, you can display a different number of products per page for your custom category while still using Magento’s built-in pager.
Remember that this approach will only work for your specific custom category. The default page size set in the admin panel will remain unchanged for all other categories.
If you’re looking to extend this functionality or need help with any other Magento development tasks, feel free to reach out to our team at Laravel Company. We specialize in providing high-quality Magento services, including custom theme development, extension creation, and performance optimization.