Hiding Elements on Specific Product List Pages in Magento
In Magento, you can target specific product list pages using URL matching or category conditions to apply CSS styles or JavaScript code. Here’s how you can achieve this to hide prices and buy buttons for certain categories.
Targeting by Category Condition
One approach is to use the category condition to apply a layout update or custom script. This can be done at the category level in your Magento admin panel:
- Navigate to Catalog > Categories in your admin.
- Select the specific category you want to target.
- Go to the Design tab for that category.
- Under Layout Update XML, add your custom layout update code, which could look like this:
<reference name="catalogsearch_result_index">
<action method="setTemplate">
<template>catalog/product/list.phtml</template>
</action>
</reference>
This example sets the template for the search results page. You can modify it to apply a custom template with your desired styling and scripting.
Targeting by URL Match
Another way is to use the URL matching feature in Magento’s layout XML files. This requires editing your theme’s local.xml file located at app/design/frontend/{package}/{theme}/layout/local.xml.
- Open the local.xml file or create it if it doesn’t exist.
- Add a new
<reference>block for catalog search results with URL matching:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:app/etc/layout.xsd">
<reference name="catalogsearch_result_index">
<action method="setTemplate">
<template>Magento_Theme::html/catalog/product/list.phtml</template>
<condition>
<url>
<eq code="category">/category-with-url-to-match</eq>
</url>
</condition>
</action>
</reference>
</config>
Replace /category-with-url-to-match with the actual URL of the category you want to target.
Customizing CSS and JavaScript
To hide prices and buy buttons, you can use CSS or jQuery in your custom template. Add a class to the price and button elements you want to hide, like this:
<div class="price">
<span class="price">$19.99</span>
</div>
<div class="actions">
<button type="submit" title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>" class="action buy primary"><span><span><?php echo $block->getButtonLabel() ?></span></span></button>
</div>
Then in your custom template or linked CSS file, use the following code:
.price,
.actions {
display: none;
}
Conclusion
To hide elements on specific product list pages in Magento, you can either target by category condition at the admin level or use URL matching in your theme’s layout XML files. By combining these techniques with custom CSS or jQuery, you can effectively remove prices and buy buttons from certain categories while keeping them visible elsewhere.
Remember to backup your files before making changes and test thoroughly after applying the updates.
For more advanced customization options, consider exploring Magento’s layout update instructions and custom template creation.
Happy coding!