Changing the default list/grid view for specific categories in Magento is a common task that requires modifying your theme’s layout XML files. Let me guide you through this process step-by-step.
Step 1: Create Grid View Template
First, you need to create a new template file for the grid view layout. In your current theme directory (usually located at /app/design/frontend/default/yourtheme), navigate to the catalog/product folder and check if it contains a grid.phtml file. If not, create one.
Here’s an example of what the grid.phtml file should look like:
<ul class="products-grid">
<?php foreach ($this->getLoadedProductCollection()->getItems() as $product): ?>
<li class="item">
<div class="product-image"><?php echo $this->helper('catalog/image')->init($_product, 'category_thumbnail'); ?></div>
<h2 class="product-name"><a href="<?php echo $this->getProductUrl($_product); ?>"><?php echo $this->htmlEscape($this->getProductName($_product)); ?></a></h2>
<div class="price-box">
<?php
if ($_product->isSaleable() && $_product->getPrice()) {
echo $this->getProductPrice($_product, true);
} else {
echo $this->helper('catalog output')->renderField($_product, 'name');
}
?>
</div>
</li>
<?php endforeach; ?>
</ul>
Step 2: Update Layout XML
Next, you need to update your category layout XML file to include the grid view. Open your theme’s catalog_category_view.xml file located in /app/design/frontend/default/yourtheme/layout.
Add or modify the following block inside the <reference name="content"> tag:
<block type="core/text_list" name="category.products" as="products">
<block type="catalog/product_grid" template="catalog/product/grid.phtml"/>
</block>
Step 3: Set Grid View for Specific Categories
Now, let’s set the grid view for specific categories. In your theme directory, navigate to /app/design/frontend/default/yourtheme/layout and create a new XML file named category_your_category_id.xml, replacing your_category_id with the actual ID of the category you want to modify.
Add the following code inside this new XML file:
<reference name="category.products">
<action method="setTemplate">
<argument name="template" xsi:type="string">catalog/product/grid.phtml</argument>
</action>
</reference>
Replace your_category_id with the actual ID of your category.
Step 4: Update Magento XML
Finally, you need to update Magento’s core XML files. Navigate to /app/code/core/Mage/Catalog/etc/design.xml and add the following line inside the <default> tag:
<layout module="catalog">
<type>
<category>
<action method="setTemplate">
<argument name="template" xsi:type="string">catalog/category/view.phtml</argument>
</action>
</category>
</type>
<category>
<your_category_id translate="label">
<label>Your Category Name Here</label>
<file>category_your_category_id.xml</file>
</your_category_id>
</category>
</layout>
Replace your_category_id with the actual ID of your category and update the <label> field accordingly.
Conclusion
By following these steps, you should now have successfully changed the default list/grid view for specific categories in Magento. Make sure to clear your cache after making these changes by running the command php bin/magento cache:flush from the terminal or using the Magento admin panel’s cache management functionality.
Remember that modifying core files directly is not recommended, as it can cause issues during updates. It’s always a good practice to create a custom theme and make your modifications there instead of touching the core files directly.
For more detailed information on Magento layout XML and customization techniques, I recommend checking out our comprehensive guide at Laravel Company.