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 2 - Create a simple module

In magento 2, the creation of module has changed completely. We will go through how to create a module. Like earlier magento, we do not need to put our module files in different folders for code, theme, skin, etc. All, the codes related to the module should be inside one main folder. You can find the sample code in the git repo .