Displaying Products on a CMS Page in Magento
As a senior developer at Laravel Company, I often receive queries from our clients about displaying products on content pages within their Magento stores. While the backend provides a static block option for this, many developers prefer to handle it directly through PHTML files for more control and flexibility.
Let’s dive into how you can achieve this by creating a new CMS page template that displays the products of a specific category.
Step 1: Create a New CMS Page
First, navigate to your Magento admin panel and go to CMS > Pages. Click on Add New Page.
Step 2: Create a Custom Template File
For this example, let’s assume you want to show the products from Category ID 4 in a new template file named custom_product_list.phtml. Here’s how you can create it:
-
Navigate to your Magento theme directory via FTP or SFTP.
- For Magento 2: Go to
<your_magento_directory>/app/design/frontend/<vendor_name>/<theme_name>/Magento_Cms/templates/page/ - For Magento 1: Go to
<your_magento_directory>/app/design/frontend/default/<template_name>/template/page/
- For Magento 2: Go to
-
Create a new file named
custom_product_list.phtml.
Step 3: Populate the Template with Products
Open your newly created custom_product_list.phtml file in your favorite text editor or IDE.
<?php
$categoryId = 4; // Replace this with your desired category ID
$collection = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();
This code loads the product collection for the specified category. Now, let’s display these products in a simple grid format.
<div class="product-grid">
<?php foreach ($collection as $_product): ?>
<div class="product-item">
<a href="<?php echo $this->getUrl('catalog/product/view', array('id' => $_product->getId())); ?>">
<?php echo $this->helper('catalog/image')->init($_product, 'category_thumbnail'); ?>
</a>
<h2><a href="<?php echo $this->getUrl('catalog/product/view', array('id' => $_product->getId())); ?>"><?php echo $this->htmlEscape($this->getImageLabel($_product, 'thumbnail')); ?></a></h2>
<?php if ($_product->getShortDescription()): ?>
<div class="short-desc"><?php echo $this->htmlEscape($_product->getShortDescription()); ?></div>
<?php endif; ?>
<p class="price">$<?php echo Mage::helper('core')->currency($_product->getFinalPrice(), true, false); ?></p>
<button type="button" class="add-to-cart button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
</div>
<?php endforeach; ?>
</div>
This code creates a simple grid layout for the products with their images, names, descriptions, and prices. It also includes an “Add to Cart” button for each product.
Step 4: Assign the New Template to Your CMS Page
Back in your Magento admin panel under CMS > Pages, find the page you created earlier. Click on it to edit the content.
Under the Content tab, select your new template file (e.g., custom_product_list.phtml) from the Template dropdown menu.
Click Save.
Conclusion
Displaying products in a CMS page allows you to create unique and engaging shopping experiences for your customers. By following this guide, you can easily add category-based product listings to any content pages within your Magento store.
Remember that this is a basic implementation. For a more advanced setup with additional filters or sorting options, consider using Laravel Company’s Magento development services for expert assistance and custom solutions tailored to your specific business needs.
Happy coding!