Displaying Products by Category on a Magento CMS Page
As an experienced developer working with e-commerce platforms like Magento, I often encounter requests to display products categorized on specific pages within the admin panel. The “products” page is a common scenario where merchants want to organize their items by category for better navigation and presentation.
Understanding the Requirement
The merchant has a dedicated “products” CMS page where they aim to display all items separated by their corresponding categories. They envision a hierarchical structure with each category appearing as a standalone section, followed by its associated products.
Category 1
Item 1 Item 2
Category 2
Item 1 Item 2
To achieve this layout, we need to leverage the power of Magento’s CMS and catalog functionality. By creating a new CMS page for “products” and using the category structure, we can dynamically populate the products within each category section.
Step-by-Step Implementation
Creating the CMS Page
- Log in to your Magento admin panel.
- Navigate to Content > Pages.
- Click on Add New Page at the top right corner.
- Enter “Products” as the page title and a suitable identifier (e.g., product-cms).
- In the Content field, we will insert our dynamic content using the category structure.
Populating Products by Category
- We’ll use Magento’s template language to dynamically generate the products for each category.
- Within the Content field, insert the following code snippet:
{{block type="catalog/product_list" category_id="$_category->getId()" template="catalog/product/list.phtml"}}
- In this block,
category_idshould be dynamic and set to$_category->getId(). Replace$_category->getId()with the appropriate variable holding the current category ID. - Save your CMS page.
Handling Dynamic Category Population
To make the category population dynamic, you can use Magento’s widget functionality in combination with a custom template file.
- Create a new template file named
products-by-category.phtmlunder your theme’s directory:app/design/frontend/{vendor}/{theme}/template/page. - In this file, you can iterate through all the categories and display them using the following code:
<?php $_categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('name'); ?>
<?php foreach ($_categories as $_category): ?>
<h2><?php echo $_category->getName(); ?></h2>
<?php $_products = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('category_id', array($_category->getId())); ?>
<?php foreach ($_products as $_product): ?>
<p><?php echo $_product->getName(); ?> - <?php echo $_product->getSku(); ?></p>
<?php endforeach; ?>
<?php endforeach; ?>
- Save the file and then use this template in your CMS page by setting the Content field to:
{{block type="core/template" template="page/products-by-category.phtml"}}
Conclusion
Displaying products categorized on a Magento CMS page is a straightforward process that leverages the platform’s built-in functionality. By creating a dedicated “products” CMS page and utilizing blocks with dynamic category IDs, you can achieve the desired hierarchical layout efficiently.
Remember to customize the template file products-by-category.phtml according to your specific design requirements and product attributes you want to display.
If you found this guide helpful, feel free to explore more Magento development resources on Laravel Company’s blog.