Hiding Bundle Items in Magento: A Comprehensive Guide
As a developer, I understand how frustrating it can be when you’re trying to customize your Magento store but are held back by unwanted elements such as bundle items on product pages. In this post, we’ll dive into the issue of hiding fixed bundle items and provide a step-by-step solution.
Understanding the Problem
By default, Magento displays all the items included in a bundled product on the product page. While this is useful for configurable options, it can make your page look cluttered and confusing for customers when there are no editable options.
The challenge lies in hiding these fixed bundle items without removing essential functionality like the ‘Add to Cart’ button.
Locating the Relevant Files
To modify the behavior of bundled products, you’ll need to navigate to the following directory:
app/design/frontend/default/my_theme/template/catalog/product/
Here, my_theme should be replaced with your current theme’s name. If you’re using a different package like Magento 2 or a custom theme, the path may vary slightly.
Identifying the Problematic Code
Upon opening the view.phtml file in a code editor, you’ll notice two key blocks of code:
<?php if ($_product->isSaleable() && $this->hasOptions()):?>
<?php echo $this->getChildChildHtml('container1', '', true, true) ?>
<?php endif;?>
<?php if ($_product->isSaleable() && $this->hasOptions()):?>
<?php echo $this->getChildChildHtml('container2', '', true, true) ?>
<?php endif;?>
The container2 block is responsible for displaying the bundled items.
A Common Pitfall
If you simply remove or comment out this container2 block, it will indeed hide the bundle items. However, this action also removes the ‘Add to Cart’ option because it’s part of the same logic that checks if there are options available.
The Correct Approach: Overriding the Template
To achieve our goal without breaking other functionality, we’ll create a copy of the view.phtml file in our theme directory:
app/design/frontend/Magento/luma/Magento_Catalog/templates/product/view.phtml
Or for Magento 2.x themes:
app/design/frontend/Magento/luma/Magento_luma/templates/catalog/product/view.phtml
Replace luma with your actual theme name.
Modifying the Template
In our new file, we’ll keep the first block of code (container1) intact. However, for the second block (container2), we’ll add a condition to check if the product is a bundle type and skip displaying it:
<?php if ($_product->isSaleable() && $this->hasOptions()):
echo $this->getChildChildHtml('container1', '', true, true);
endif;?>
<!-- This checks if the product is of type Bundle -->
<?php if ($blockType = $_product->getTypeId() && $blockType == 'bundle'):
// Skip displaying bundled items
else: ?>
<?php echo $this->getChildChildHtml('container2', '', true, true); ?>
<?php endif;?>
This approach ensures that the bundle items are only displayed if they’re editable (i.e., not fixed) and preserves the ‘Add to Cart’ functionality.
Conclusion
Hiding fixed bundle items in Magento requires a nuanced understanding of how product templates work. By following this guide, you can clean up your product pages without compromising essential features like adding items to the cart.
Remember, always backup your files before making changes and test thoroughly on a staging environment first. If you’re looking for more advanced customization techniques or need professional assistance with your Magento store, feel free to reach out to Laravel Company for expert support.
Happy coding!