{"id":75,"date":"2018-04-09T00:48:34","date_gmt":"2018-04-09T00:48:34","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2018\/04\/09\/get-magento-2-attribute-and-options-programmatically\/"},"modified":"2025-04-17T11:25:57","modified_gmt":"2025-04-17T05:55:57","slug":"get-magento-2-attribute-and-options-programmatically","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-magento-2-attribute-and-options-programmatically\/","title":{"rendered":"How to Get Magento 2 Attribute &#038; Attribute Options Programmatically"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">For Magento 2, attributes are options to describe the products and its features. It is a property of the product. It helps potential customers to choose the best-fit product for their needs. By default, Magento 2 products have predefined attributes like name, price, description etc. To show the characteristics of a product, custom options are created in the store. The value of attributes is called attribute options.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Default Magento does not allow to get Magento 2 attribute &amp; attribute options. If sometimes you need to get the attribute &amp; attribute options to set somewhere, you can get the values by custom coding it. Today, I have come up with a custom code to\u00a0get Magento 2 attribute &amp; attribute options programmatically!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to get Magento 2 attribute &amp; attribute options programmatically:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">use Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute;\nuse Magento\\Catalog\\Model\\Product\\Attribute\\Repository;\n\nprotected $attribute;\nprotected $attributeRepository;\n\npublic function __construct(\n        Attribute $attribute,\n    Repository $attributeRepository)\n{\n    $this->attribute = $attribute;\n    $this->attributeRepository = $attributeRepository;\n}\n\npublic function getAttributeOption($attributeId)\n{\n    $attributeModel = $this->attribute->load($attributeId);\n    $attributeCode = $attributeModel->getAttributeCode();\n    $options = $this->attributeRepository->get($attributeCode)->getOptions();\n    return $options;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Execute the above code and get attribute &amp; attribute options programmatically in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hope we have been helpful to get it done.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you find any difficulty in the execution of the code or stuck somewhere,&nbsp;do let me know by commenting below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I\u2019ll be happy to help!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Found the article good enough?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Don\u2019t forget to flash 5 stars  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>For Magento 2, attributes are options to describe the products and its features. It is a property of the product. It helps potential customers to&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-75","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/75","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=75"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"predecessor-version":[{"id":12300,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/75\/revisions\/12300"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}