Magento attributes - get admin label for attribute options

Posted by Damodar Bashyal on July 16, 2013

 

We had custom module to display color boxes instead of color labels for colour selection. In the magento admin we had set label 'White' for admin / default but '#ffffff' for magento store frontend, so we can use it on style background color. See image for more details about this requirement and issue.

magento default attribute color options label

But the issue was on mouse over it was displaying HEX color code instead of label white. So, we added below code on the tcatalog helper and we were able to get correct store label we were expecting from magento.

    /*
	 * $attributeCode = 'color'
	 * $item = color option item object
	 */
	public function getAttributeAdminLabel($attributeCode, $item){
        ///trunk/app/code/core/Mage/Eav/Model/Config.php
        $entityType = Mage::getModel('eav/config')->getEntityType('catalog_product');
        //$entityTypeId = $entityType->getEntityTypeId();
        $attributeModel = Mage::getModel('eav/entity_attribute')->loadByCode($entityType, $attributeCode);
        $_collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
                          ->setAttributeFilter($attributeModel->getId())
                        ->setStoreFilter(0)
                        ->load();

        foreach( $_collection->toOptionArray() as $_cur_option ) {
            if ($_cur_option['value'] == $item->getValue()){ return $_cur_option['label']; }
        }
        return $item->getLabel();
    }

Now, we could use this solution to call it as:

$alt = Mage::helper('tcatalog')->getAttributeAdminLabel($attribute, $_item);

Now we can use this $alt default magento label in our configurable product's option selection.

/trunk/app/design/frontend/enterprise/responsive/template/catalog/layer/filter.phtml

<ol>
<?php foreach ($this->getItems() as $_item): ?>
    <?php
    if(!$attribute){
        $attribute = $_item->getFilter()->getRequestVar();
    }
    if($attribute == 'color'){
        $alt = Mage::helper('tcatalog')->getAttributeAdminLabel($attribute, $_item);
        $_item->setAltTitle($alt);
        $_item->setLabel('<span class="color" style="background-color: ' . $_item->getLabel() . '"></span>');
    }
    ?>
    <li class="<?php echo $attribute ?>">
        <?php if ($_item->getCount() > 0): ?>
        <a href="<?php echo $this->urlEscape($_item->getUrl()) ?>" title="<?php echo $_item->getAltTitle() ?>"><?php echo $_item->getLabel() ?></a>
        <?php else: echo $_item->getLabel() ?>
        <?php endif; ?>
        <?php if ($this->shouldDisplayProductCount()): ?>
        (<?php echo $_item->getCount() ?>)
        <?php endif; ?>
    </li>
<?php endforeach ?>
</ol>

For tcatalog extension visit: https://github.com/dbashyal/Magento_Tcatalog/blob/master/app/code/community/Technooze/Tcatalog/Helper/Data.php

 
not published on website


QR Code: Magento attributes - get admin label for attribute options