Thursday, 25 October 2012

How to target CSS for different product types





To normally target a certain CSS specificity of a page in Magento, you would reference a class in the <body> tag (.cms-index-index, .catalog-product-view, etc.). However, there isn't a way to target the specificity of a certain product type.

There is almost always more than one way to do something in Magento. One option would be to create a custom extension, which taps into Magento's process of creating a custom class name and outputting it onto the <body> html's class attribute. This option, however, would involve an experienced Magento programmer to create this extension.

Another option is to target the product type layout handle, and add a separate CSS file for that product type. To do this, add a PRODUCT_TYPE_handle to your app/design/frontend/yourpackage/default/layout/local.xml file:

<?xml version="1.0"?>
<layout version="0.1.0">
    ...
    <PRODUCT_TYPE_handle>
        <reference name="head">
            <action method="addCss"><stylesheet>css/PRODUCT_TYPE_handle.css</stylesheet></action>
        </reference>
    </PRODUCT_TYPE_handle>
    ...
</layout>





Then, create a file at skin/frontend/yourpackage/default/css/PRODUCT_TYPE_handle.css, and add in your appropriate CSS which you want specific to this product type. Replace PRODUCT_TYPE_handle with the product type you would like to override.

Since this update involves updating XML, be sure to clear the cache in order to rebuild the XML layer and see your changes.

Magento Product Types and Layout Handles




Product typeLayout update handle
SimplePRODUCT_TYPE_simple
ConfigurablePRODUCT_TYPE_configurable
GroupedPRODUCT_TYPE_grouped
VirtualPRODUCT_TYPE_virtual
DownloadablePRODUCT_TYPE_downloadable
BundlePRODUCT_TYPE_bundle



No comments :

Post a Comment