Displaying Custom Product Options on Magento Category List Page
In Magento, adding custom options to products is a powerful feature that allows you to offer variations like sizes or colors. By default, these options are visible only on the individual product page and when a customer adds an item to their cart.
However, there are times when you might want to display these options directly on the category list page for better visibility and user experience. This can be especially useful if your store has many configurable products.
Understanding the Magento Product Architecture
Before we dive into the code changes, let’s quickly review how Magento structures product data:
- Product: The base product entity that contains general information like name, description, price, etc.
- Configurable Product: A type of simple product with associated child products representing various options (like size or color).
- Child Products: Simple products linked to the configurable parent, each representing a specific option.
Displaying Custom Options on Category List Page
To display custom options on the category list page, we need to modify two key files:
1. Override Category Product Collection
In your theme’s local Magento directory (e.g., app/design/frontend/{YourVendor}/{YourTheme}), create a new folder called Magento_Catalog and navigate to the Model/ResourceModel/Product/Collection.php file.
Make a copy of this file in your theme directory. Then, open the copied file in a text editor.
Find the method addVisibilityFilter() around line 136 (the exact line number may vary slightly depending on your Magento version).
Add the following code snippet after the existing conditions:
->addAttributeToSelect('required_options')
->addAttributeToSelect('custom_options');
This tells Magento to select the required options and custom options for each product.
2. Modify Category Product List Template
Next, navigate to app/design/frontend/{YourVendor}/{YourTheme}/Magento_Catalog/templates/product/list.phtml.
Add the following PHP conditional statement below the loop that starts at line 63:
<?php if ($block->isShowCustomOptionsEnabled()): ?>
<?php foreach ($block->getProduct()->getTypeInstance(true)->getConfigurableAttributesAsArray($product) as $optionId => $option): ?>
<div class="options">
<strong>Custom Options:</strong>
<ul>
<?php foreach ($option['values'] as $value): ?>
<li><?php echo $this->escapeHtml($value['label']) ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
<?php endif; ?>
This code checks if the isShowCustomOptionsEnabled() method (which we’ll implement next) returns true. If it does, it fetches and displays the custom options for each product.
3. Implement a New Method in Your Theme’s Category View Block
Create a new file at app/design/frontend/{YourVendor}/{YourTheme}/Magento_Catalog/view/frontend/templates/product/list.phtml (if it doesn’t exist already).
Add the following method to your block:
public function isShowCustomOptionsEnabled()
{
return true; // or whatever logic you want here
}
This method determines whether custom options should be displayed. For now, we’ve simply set it to always return true.
Testing Your Changes
Flush Magento’s cache and then navigate to a category listing page.
You should now see the custom product options listed below each product on the category page.
Conclusion
Displaying custom options directly on the category list page in Magento involves overriding core files, which is a departure from the recommended extension development methodology. However, for small-scale projects or specific use cases, this approach provides a quick and effective solution.
Remember to keep track of your changes using version control systems like Git. This ensures you can easily revert if necessary and maintain your store’s stability.
For more advanced customizations and long-term scalability, consider developing your own Magento extension following the official Extension Development Guide.
Happy coding!