{"id":7178,"date":"2019-09-18T16:33:05","date_gmt":"2019-09-18T11:03:05","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7178"},"modified":"2019-09-19T18:47:08","modified_gmt":"2019-09-19T13:17:08","slug":"magento-2-get-product-attribute-id-name-options-from-attribute-code","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-product-attribute-id-name-options-from-attribute-code\/","title":{"rendered":"Magento 2 &#8211; How to get Product Attribute Id, Name, Options from Attribute Code"},"content":{"rendered":"<p>In this post I am going to explain how to get the attribute information from attribute code in magento 2.<\/p>\n<p><strong>1. Using Dependency Injection<\/strong><\/p>\n<p>Add below code snippet in Block class.<\/p>\n<pre class=\"lang:default decode:true \">protected $_entityAttribute;\r\nprotected $_attributeOptionCollection;\r\n\r\npublic function __construct(\r\n\t\\Magento\\Backend\\Block\\Template\\Context $context,\r\n\t\\Magento\\Eav\\Model\\Entity\\Attribute $entityAttribute,\r\n\t\\Magento\\Eav\\Model\\ResourceModel\\Entity\\Attribute\\Option\\Collection $attributeOptionCollection,\r\n\tarray $data = []\r\n) \r\n{\r\n\t$this-&gt;_entityAttribute = $entityAttribute;\r\n\t$this-&gt;_attributeOptionCollection = $attributeOptionCollection;\r\n\tparent::__construct($context, $data);\r\n}\r\n\r\npublic function getAttributeData($entityType, $attributeCode) \r\n{\r\n\t$attributeData = $this-&gt;_entityAttribute\r\n\t\t\t\t-&gt;loadByCode($entityType, $attributeCode);\r\n\treturn $attributeData;\r\n}\r\n\r\npublic function getAttributeOptions($attributeId) \r\n{\r\n\t$attributeOptions = $this-&gt;_attributeOptionCollection\r\n\t\t\t\t-&gt;setPositionOrder('asc')\r\n\t\t\t\t-&gt;setAttributeFilter($attributeId)\r\n\t\t\t\t-&gt;setStoreFilter()\r\n\t\t\t\t-&gt;load();\r\n\treturn $attributeOptions;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Add below code snippet in template file.<\/p>\n<pre class=\"lang:default decode:true \">\/\/attribute code\r\n$attributeCode = 'manufacturer';\r\n\/\/entity type\r\n$entityType = 'catalog_product';\r\n\r\n$attributeData = $block-&gt;getAttributeData($entityType, $attributeCode);\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($attributeData-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n\r\n\/\/ get attribute id\r\n$attributeId = $attributeData-&gt;getAttributeId();\r\n\/\/ get attribute name\r\n\r\necho $attributeData-&gt;getFrontendLabel();\r\n\r\n\/\/ get attribute default value\r\necho $attributeData-&gt;getDefaultValue();\r\n\r\n\/\/get attribute options\r\n$attributeOptions = $block-&gt;getAttributeOptions($attributeId);\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($attributeOptions-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n\r\nforeach ($attributeOptions as $option) {\r\n    echo $option-&gt;getOptionId() . \"&lt;br&gt;\";\r\n    echo $option-&gt;getValue() . \"&lt;br&gt;\";\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>2. Using Object Manager<\/strong><\/p>\n<pre class=\"lang:default decode:true\">\/\/attribute code\r\n$attributeCode = 'manufacturer';\r\n\/\/entity type\r\n$entityType = 'catalog_product';\r\n\r\n$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\r\n\r\n$attributeData = $objectManager-&gt;get(\\Magento\\Eav\\Model\\Entity\\Attribute::class)\r\n        -&gt;loadByCode($entityType, $attributeCode);\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($attributeData-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n\r\n\/\/ get attribute id\r\n$attributeId = $attributeData-&gt;getAttributeId();\r\n\r\n\/\/ get attribute name\r\necho $attributeData-&gt;getFrontendLabel();\r\n\r\n\/\/get attribute default value\r\necho $attributeData-&gt;getDefaultValue();\r\n\r\n\/\/get attribute options\r\n$attributeOptions = $objectManager-&gt;get(\\Magento\\Eav\\Model\\ResourceModel\\Entity\\Attribute\\Option\\Collection::class)\r\n        -&gt;setPositionOrder('asc')\r\n        -&gt;setAttributeFilter($attributeId)\r\n        -&gt;setStoreFilter()\r\n        -&gt;load();\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($attributeOptions-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n\r\nforeach ($attributeOptions as $option) {\r\n    echo $option-&gt;getOptionId() . \"&lt;br&gt;\";\r\n    echo $option-&gt;getValue() . \"&lt;br&gt;\";\r\n}<\/pre>\n<p>Thats it. Enjoy Magento 2!!<\/p>\n<p>Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to get the attribute information from attribute code in magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_entityAttribute; protected $_attributeOptionCollection; public function __construct( \\Magento\\Backend\\Block\\Template\\Context $context, \\Magento\\Eav\\Model\\Entity\\Attribute $entityAttribute, \\Magento\\Eav\\Model\\ResourceModel\\Entity\\Attribute\\Option\\Collection $attributeOptionCollection, array $data = [] ) { $this-&gt;_entityAttribute = $entityAttribute; $this-&gt;_attributeOptionCollection =&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-product-attribute-id-name-options-from-attribute-code\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7212,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[653,654,655,10,28,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7178"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=7178"}],"version-history":[{"count":5,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7178\/revisions"}],"predecessor-version":[{"id":7213,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7178\/revisions\/7213"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7212"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}