1

I have 1 category, and 2 attributes which i can sort by, for example boys and girls and i would like the products that have 1 attribute to have certain colors, for example blue for boys and pink for girls for certain things like product name and others.

2
  • do you want to change in the product view page right?
    – Mujahidh
    Oct 28, 2015 at 7:20
  • @jay did you manage to find a solution to your question? Nov 10, 2015 at 14:19

3 Answers 3

1

The easiest solution would be to have an overall class you can watch. For example an extra body class. We can add that with the event core_block_abstract_to_html_before.

Your config.xml would have the following observer

<config>
    [...]
    <frontend>
        [...]
        <events>
            <core_block_abstract_to_html_before>
                <observers>
                    <[namespace]_[module]_core_block_abstract_to_html_before>
                        <type>singleton</type>
                        <class>[Namespace]_[Module]_Model_Observer</class>
                        <method>coreBlockAbstractToHtmlBefore</method>
                    </[namespace]_[module]_core_block_abstract_to_html_before>
                </observers>
            </core_block_abstract_to_html_before>
        </events>
        [...]
    </frontend>
    [...]
</config>

And you [Namespace]/[Module]/Model/Observer.php class would look like

class [Namespace]_[Module]_Model_Observer
{
    public function coreBlockAbstractToHtmlBefore($o)
    {
        $this->_addBodyClass($o);
        return $this;
    }

    protected function _addBodyClass($o)
    {
        $_block = $o->getBlock();
        $_product = Mage::registry('current_product');
        if ($_block instanceof Mage_Page_Block_Html && $_product instanceof Mage_Catalog_Model_Product) {
            $block->addBodyClass('sex-'.$_product->getData('sex'));
        }
    }
}
0

Get your attribute in the product page.You can check the attribute from that and according to your attribute you can call the css classes.

$atr = $product->getAtribute();
//You can retrieve the css class according to your product attribute in phtml file
<h1 <?php if($atr=="boys"){?>class="product-title-boys" <?php }
          elseifif($atr=="girls"){?> class="product-title-Girls"<?php} ?>>
          <?php echo $product->getName(); ?>
</h1>
0

as in year 2022 you don't need to add customised module to achieve this as there are two useful body classes added automatically and smartly/dynamically (using browsing sequences) by Magento core, on both catalog entities, i.e. category page and product page.

One example is like following,

<body class="catalog-product-view product-plan-toys-tableware categorypath-products-eco-toys category-eco-toys page-layout-1column preloaded">
    ...
</body>

This is done by Magento core as in version 2.4 (not sure earlier versions as lack of experience) through vendor/magento/module-catalog/Controller/Category/View.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.