Magento Pagination for Custom Product Collection Inside PHTML File
In this post, we’ll address a common scenario where you need to implement pagination for a custom product collection directly inside a PHTML file in Magento without creating a separate module. While this approach isn’t ideal from an architectural standpoint, it’s important to understand how to handle such situations, especially when working with third-party extensions.
The Current Implementation
The developer has already attempted to load the product collection within the list_all.phtml file using a simple query:
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addStoreFilter()
->addUrlRewrite()
->addAttributeToFilter('groupdeal_status', Devinc_Groupdeals_Model_Source_Status::STATUS_RUNNING)
->joinField('groupdeals_id','groupdeals/groupdeals','groupdeals_id','product_id=entity_id',null,'left');
$collection->getSelect()->limit(2);
They also tried to generate the pagination using Magento’s built-in toolbar block:
$toolbar = Mage::getBlockSingleton('catalog/product_list')->getToolbarBlock();
$toolbar->setCollection($collection);
echo $toolbar->toHtml();
However, this code only displays the product count and “Show per page” options without the actual pagination links (1, 2, 3…).
Understanding the Issue with Pagination Links
The issue lies in the way Magento initializes the toolbar block. The catalog/product_list block is intended to be used within a layout file and has dependencies on other blocks like catalog_category_view. When you try to initialize it directly in a PHTML file, those dependencies aren’t resolved correctly.
A Better Approach Without Creating a Module
While creating a custom module would provide the cleanest solution, you can achieve pagination without one by following these steps:
-
Create a new file named
catalog/product/list_all.phtmlunder your current theme directory (if it doesn’t exist). -
Copy and paste the existing collection query into this new PHTML file.
-
Instead of using the toolbar block directly, create a custom pagination:
// Set the total number of products in the collection
$totalItems = $collection->getSize();
if ($totalItems > 0) {
// Set the current page (default to 1 if not specified)
$currentPage = isset($_GET['p']) ? intval($_GET['p']) : 1;
// Calculate the total number of pages
$totalPages = ceil($totalItems / $collection->getLimitedSize());
echo '<div class="pagination">';
for ($i = 1; $i <= $totalPages; $i++) {
if ($currentPage == $i) {
echo "<span class='active'>$i</span>";
} else {
echo "<a href='?p=$i'>$i</a>";
}
}
echo '</div>';
}
This code calculates the total number of pages based on the collection size and displays numbered links for each page.
- Finally, you need to update your category template’s design tab:
<reference name="product_list">
<action method="setTemplate" template="catalog/product/list_all.phtml"/>
</reference>
Conclusion
While it’s not recommended to handle such operations directly in PHTML files due to the potential for messy code and scalability issues, this approach can be a viable quick-fix when working with existing third-party extensions.
Remember, creating a custom module would provide better separation of concerns and easier maintenance, especially as your project grows. It’s also an excellent opportunity to learn more about Magento’s extension development practices.
For a deeper dive into Magento extension development best practices, consider checking out the LaravelCompany Blog for comprehensive guides and tutorials.