Customizing Magento Product Page Design for Group & Bundle Types
As an experienced Magento developer, I often receive requests for customizing product pages based on their types - specifically group, bundle, and configurable products. While assigning different templates is one approach, it’s not always feasible due to certain limitations or requirements in your project.
Let me guide you through the process of rendering specific view.phtml files (custom_view.phtml and custom_view2.phtml) for bundle and group product types respectively without using template assignment.
Step 1: Understand the Product Type
In Magento, each product type is associated with a specific controller that handles its display logic. For example:
- Group products are handled by
catalog/product/view/groupAction - Bundle products are handled by
catalog/product/view/bundleAction
Step 2: Override the Action
To customize the view for these product types, you’ll need to override their respective actions and modify the layout accordingly.
For group products, create a new XML file at app/design/frontend/{package}/{theme}/layout/catalog_product_view_group.xml with the following content:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="catalog_product_view"/>
<referenceContainer name="content">
<block class="Magento\Catalog\Block\Product\View" template="custom_group_view.phtml"/>
</referenceContainer>
</page>
For bundle products, create app/design/frontend/{package}/{theme}/layout/catalog_product_view_bundle.xml:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="catalog_product_view"/>
<referenceContainer name="content">
<block class="Magento\Bundle\Block\Product\View" template="custom_bundle_view.phtml"/>
</referenceContainer>
</page>
Replace {package} and {theme} with your actual Magento package and theme names.
Step 3: Create the Custom Templates
Create the custom templates custom_group_view.phtml and custom_bundle_view.phtml in your theme directory under app/design/frontend/{package}/{theme}/template/catalog/product/view.
In these files, you can now write your custom HTML and use Magento blocks to retrieve product data. For example:
<!-- custom_group_view.phtml -->
<div class="group-product-container">
<h1><?= $block->getProduct()->getName() ?></h1>
<!-- Add your custom HTML and Magento blocks here -->
</div>
Step 4: Clear Cache
Finally, clear the cache to ensure that your changes take effect. You can do this from the Magento admin panel under System > Cache Management or by running the following commands in your terminal:
php bin/magento cache:clean
php bin/magento cache:flutter
Conclusion
Customizing product pages for specific types like group and bundle products requires a deep understanding of the Magento architecture. By overriding the layout XML files and creating custom templates, you can achieve the desired design changes without relying on template assignment.
Remember to keep your code clean, efficient, and modularized for better maintainability and scalability.
For more advanced customizations and best practices in Magento development, I recommend exploring the resources available at Laravel Company’s Magento blog.
Happy coding!