How could I get Attribute model
(from: eav_attribute
table) by attribute_code
in Magento?
Notice:
- I don't care what is entity_type.
Thank you so much.
Just instantiate the Magento\Eav\Api\AttributeRepositoryInterface class to retrieve the id value. Here, We are fetching Product SKU attribute Id programmatically. You can pass any attribute value you want to retrieve with the entity type. $getId = $this->getAttributeId();
You can use anywhere this code by inject \Magento\Eav\Model\Entity\Attribute class in your construct. After that, you can call getCustomAttributeCode() function to get attribute code.
You have to know entity_type
because you can have the same attribute_code
for different entities. So to get attribute model:
$attributeModel = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type, $attributeCode);
$entity_type
parameter can be numeric
(id directly), string
(for example 'catalog_product'
or Mage_Catalog_Model_Product::ENTITY
) or it can be instance of model Mage_Eav_Model_Entity_Type
Perhaps you can read the attributes by filter the collection
Mage::getModel('eav/entity_attribute')->getCollection()->addFieldToFilter('attribute_code', array('in' => $codes) )
Since I need the attributes from the Product by code, I do it like this:
$codes = (array) $codes;
$res = array_intersect_key($this->getAttributes(), array_flip($codes));
$codes is an attribute_code-array Scope: extended Mage_Catalog_Model_Product
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With