Magento : Display html element based on product attribute set?

Magento : Display html element based on product attribute set?

October 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento : Display html element based on product attribute set?

[Blog Post]

Magento: Conditional Display of Static Blocks Based on Product Attribute Set

You’re not alone if you’ve found yourself wanting to display static blocks conditionally based on the product attribute set in Magento. This is a common requirement, especially for stores with diverse product categories like fashion shops that need to differentiate between footwear and apparel.

The Challenge: Displaying Static Blocks Based on Attribute Set

Let’s say you’re running an online fashion store and want to display a static block only when the current product belongs to the “Footwear” attribute set. You may have found some code snippets that output the ID of the attribute set, but turning this into a working conditional statement can be tricky.

The key here is to understand how Magento handles attribute sets and to use its built-in functionality effectively.

Using Magento’s Built-In Functions

Your starting point is using Magento’s Model classes (Mage::getModel) to retrieve information about the current product’s attribute set. The code you provided is a good beginning:

$entityTypeId = Mage::getModel('eav/entity')
                ->setType('catalog_product')
                ->getTypeId();
$attributeSetName   = 'Footwear';
$attributeSetId     = Mage::getModel('eav/entity_attribute_set')
                    ->getCollection()
                    ->setEntityTypeFilter($entityTypeId)
                    ->addFieldToFilter('attribute_set_name', $attributeSetName)
                    ->getFirstItem()
                    ->getAttributeSetId();
echo $attributeSetId;

This script retrieves the ID of the attribute set with the name “Footwear” for a product. However, to display or hide static blocks based on this information, you need to integrate it into your Magento theme’s layout files.

Conditional Display in Your Theme Layout XML

To conditionally display a static block based on an attribute set, you’ll need to use Magento’s layout update XML functionality. You can create a new layout file (for example, catalog_product_view.xml) and add an if statement to check the current product’s attribute set.

Here’s how you might do it:

<layout version="0.1.0">
    <update handle="catalog_product_view"/>

    <reference name="content">
        <block type="core/template" template="yourtheme/footwear_conditional.phtml"/>
    </reference>
</layout>

In this example, we’re loading a custom PHTML file (footwear_conditional.phtml) that will contain the conditional logic.

The Footwear_Conditional.Phtml File

In your footwear_conditional.phtml file, you can use Magento’s object manager to retrieve the current product and check its attribute set. Here’s how:

<?php
$product = Mage::registry('current_product');

if ($product->getAttributeSetId() == Your_Desired_Set_ID) {
    echo $this->getLayout()->createBlock('cms/block')->setBlockId('Your_Static_Block_ID')->toHtml();
}
?>

Replace Your_Desired_Set_ID with the ID of your desired attribute set, and replace Your_Static_Block_ID with the ID of your static block.

Conclusion

Displaying or hiding Magento static blocks based on product attribute sets involves understanding how to retrieve the attribute set information using Magento’s built-in Model classes and then integrating this information into your theme’s layout XML. By creating a custom PHTML file for conditional logic, you can achieve the desired behavior of showing specific content only when certain conditions are met.

Remember to always test changes thoroughly on a staging environment before deploying them live. And as with any Magento development task, staying up-to-date with the latest security patches and best practices is crucial see our guide at Laravel Company for more details.

Happy coding! Let me know if you have any further questions.

Gina

Catalog

Our network