Magento: Add Layout Handle for Product Attribute Sets

Out of the box, Magento includes layout handles for different product types, simple, grouped, bundle, etc in order to add or change blocks shown on the product page.

Sometimes however, you may want to show different blocks or change the look of a product page. Magento does include a layout handle for each Product ID that you may use, but what if you wanted to use the same changes on all products having a certain Attribute Set? We can extend the Catalog_Helper_Product_View class and update the initProductLayout method to add a layout for the Attribute Set.

The following code will update the Product View helper to add a layout handle we can use. For example, if your attribute set was named  Laptops you would have the ability to use <PRODUCT_ATTRIBUTE_SET_Laptops> in your catalog.xml layout file.

First, under app/code/local create a folder for the module, for this example we will create the folder SW. Under the SW folder, create the following folders, Catalog, Catalog/etc, Catalog/Helper/Product. We will end up with the following structure.

app/
  code/
    local/
      SW/
        Catalog/
          Helper/
            Product/
              View.php
            etc/
              config.xml

Config.xml

View.php

 

Note: I originally posted this tutorial May 23rd, 2009. I updated the code to match Magento 1.6 which is the version I am currently using. Another method for handling this is found at Magebase – Creating Custom Layout Handles. Their method uses an observer to add the handle before the layout is loaded, which is better than extended the core code, as you won’t run the risk of collisions with other modules that might try and override the same code. I decided to post this anyway, mostly for posterity.

Leave a Reply