Adding Page Number to Title on Magento Products Listing
As an experienced Magento developer, I often encounter scenarios where clients need to display page numbers in the title for product listings created through extensions like the Attribute Info Pages extension by Zenoooo. In this post, we’ll explore how to achieve this without breaking the grid functionality.
The Problem: Breaking Grid Functionality
You’ve already attempted to get the current and last page information using Magento’s page/html_pager block. However, your code sets a fixed limit of 8 items per page regardless of the user’s “Show per page” selection.
This occurs because you’re manually setting $html_pager->setLimit($limit) to a fixed value after checking if the limit is empty. This override breaks the grid’s pagination and always shows 8 products, even when the user selects a different limit.
The Solution: Using $_GET Parameter
To solve this issue while preserving the grid’s original functionality, we should retrieve the “limit” parameter directly from the GET request instead of setting it manually.
Here’s how to modify your code:
$html_pager = Mage::getBlockSingleton('page/html_pager');
$html_pager->setCollection($product_collection);
// Retrieve limit from GET request
$limit = (int) Mage::app()->getRequest()->getParam('limit', 8); // Default to 8 if not provided
// Set collection size without overriding grid's pagination
$collectionSize = $product_collection->getSize();
if ($collectionSize > 0 && $limit >= $collectionSize) {
$limit = $collectionSize;
}
$html_pager->setLimit($limit);
In this updated code, we first retrieve the “limit” parameter from the GET request using Mage::app()->getRequest()->getParam('limit'). If no limit is provided, it defaults to 8. We then compare this value with the collection size and set the limit accordingly.
Calculating Page Information
Next, you can calculate the page information without breaking the grid:
$LastPageNumber = $html_pager->getLastPageNum();
$current_page = $html_pager->getCurrentPage();
$page_info = "";
if ($current_page > 1) {
$page_info = "Page " . $current_page . " of " . $LastPageNumber . " for ";
}
// ... (rest of your code)
Setting the Page Title and Description
Finally, you can set the page title and description using the calculated information:
$head = $this->getLayout()->getBlock('head');
$title = $page_info . $original_title;
$des = $items_info . $original_description;
$head->setTitle($title);
$head->setDescription($des);
Conclusion
To add page numbers to the title of a Magento product listing without breaking the grid functionality, you should:
- Retrieve the “limit” parameter from the GET request instead of setting it manually.
- Compare this limit with the collection size and set the limit accordingly to preserve pagination.
- Calculate the current and last page information using the
page/html_pagerblock’s methods. - Set the title and description using the calculated page information along with the original title and description.
Following these steps ensures that your custom page number addition works seamlessly with Magento’s built-in grid pagination functionality, providing a better user experience for your shoppers.
For more advanced tips on Magento development and extensions, consider exploring our comprehensive guides at Laravel Company.