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 Attributes in Setup

Sometimes in our module we require to create product or category attributes automatically on module install. Also, we might require to add extra fields to quote, sales order, invoice, creditmemo tables. We will go through how to do that in magento 2. You can find the sample code in the git repo .