Skip to main content

Magento - Get the attributes from attribute code


We always require the attribute data via attribute code. Below is the code to get the product attribute data from the attribute code. It can used to get the attribute data from the code, without always trying to load the attribute model.



public function getAttribute($code) {
    $this->getAttributes();
    if (isset($this->_attributes[$code])) {
        return $this->_attributes[$code];
    }
    return null;
}

public function getAttributes() {
    if (is_null($this->_attributes)) {
        $this->_attributes = Mage::getResourceSingleton("catalog/product")
                ->loadAllAttributes()
                ->getAttributesByCode();
    }
    return $this->_attributes;
}

Comments

Popular posts from this blog

Magento - Unit Testing

Magento 2 comes with default unit testing in built. But, for magento 1, it is very difficult to perform unit testing. There is an extension EcomDev_PHPUnit, which can help us in performing unit tests in magento 1.