23

How can I get the attribute options values of eav entity?
I found solution only for magento 1.x but M2 I don't know.
M1:

$attr = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('specialty')->getData()[0];
$attributeModel = Mage::getModel('eav/entity_attribute')->load($attr['attribute_id']);
$src =  $attributeModel->getSource()->getAllOptions();

Anyone know, show me step by step, pls!Thanks!

2
  • I didn't get it Can you give an example of a working code where you get magento attribute parameters programmatically Like here pearlbells.co.uk/code-snippets/… it didn't work for me. Commented Mar 22, 2021 at 7:44
  • -- Divya I didn't succeed with your code. Here is a working example for finding product data <?php use Magento\Framework\App\Bootstrap; require realpath(DIR) . '/../app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $obj = $bootstrap->getObjectManager(); $state = $obj->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); $sku ='YL-YM-101'; // my product sku $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productObject = $objectManager->get('Magento\Catalog\Model\Product'); $product = $productObject->loadByAttribute('sku', $sku); echo $p Commented Mar 30, 2021 at 14:04

6 Answers 6

75

you can add to the constructor of your class an instance of \Magento\Eav\Model\Config like this:

protected $eavConfig;
public function __construct(
    ...
    \Magento\Eav\Model\Config $eavConfig,
    ...
){
    ...
    $this->eavConfig = $eavConfig;
    ...
}

then you can use that in your class

$attribute = $this->eavConfig->getAttribute('catalog_product', 'attribute_code_here');
$options = $attribute->getSource()->getAllOptions();
10
  • How to get "value" and "label"?
    – MrTo-Kane
    Commented Mar 8, 2016 at 8:19
  • 1
    see how the result looks like. Var dump it or something.
    – Marius
    Commented Mar 8, 2016 at 8:26
  • array(2) { [0]=> array(2) { ["value"]=> int(1) ["label"]=> object(Magento\Framework\Phrase)#1504 (2) { ["text":"Magento\Framework\Phrase":private]=> string(7) "Enabled" ["arguments":"Magento\Framework\Phrase":private]=> array(0) { } } } [1]=> array(2) { ["value"]=> int(2) ["label"]=> object(Magento\Framework\Phrase)#1494 (2) { ["text":"Magento\Framework\Phrase":private]=> string(8) "Disabled" ["arguments":"Magento\Framework\Phrase":private]=> array(0) { } } } }
    – MrTo-Kane
    Commented Mar 8, 2016 at 8:29
  • 12
    Small but important remark: If available, better to use Module Service Layer. For eav attributes it's \Magento\Eav\Api\Attribute RepositoryInterface. Anything not marked as @api treated as private and can be removed in minor releases.
    – KAndy
    Commented Mar 8, 2016 at 11:54
  • 5
    @KAndy Good remark. You can write that as an answer. I think it's far better than mine.
    – Marius
    Commented Mar 8, 2016 at 12:16
10

Use the following code to get all attribute options.

function getExistingOptions( $object_Manager ) {

$eavConfig = $object_Manager->get('\Magento\Eav\Model\Config');
$attribute = $eavConfig->getAttribute('catalog_product', 'color');
$options = $attribute->getSource()->getAllOptions();

$optionsExists = array();

foreach($options as $option) {
    $optionsExists[] = $option['label'];
}

return $optionsExists;

 }

Please Can you click here for more detailed explanation. http://www.pearlbells.co.uk/code-snippets/get-magento-attribute-options-programmatically/

6

I use the Api Service Layer Magento\Eav\Api\AttributeRepositoryInterface suggested by @kandy in comments on @marius answer.

Inject the service data member in your constructor as follow.

protected $eavAttributeRepository;
public function __construct(
    ...
    \Magento\Eav\Api\AttributeRepositoryInterface $eavAttributeRepositoryInterface,
    ...
){
    ...
    $this->eavAttributeRepository = $eavAttributeRepositoryInterface;
    ...
}

And the you can get the attribute using this.

$attribute = $this->eavAttributeRepository->get(
    \Magento\Catalog\Model\Product::ENTITY,
    'attribute_code_here'
);
// var_dump($attribute->getData()); 

In order to get attribute option values array, use this.

$options = $attribute->getSource()->getAllOptions();
5

You can do it simply call below code inside your Block file.

<?php
namespace Vendor\Package\Block;

class Blockname extends \Magento\Framework\View\Element\Template
{
    protected $_productAttributeRepository;

    public function __construct(        
        \Magento\Framework\View\Element\Template\Context $context,   
        \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository,
        array $data = [] 
    ){        
        parent::__construct($context,$data);
        $this->_productAttributeRepository = $productAttributeRepository;
    } 

    public function getAllBrand(){
        $manufacturerOptions = $this->_productAttributeRepository->get('manufacturer')->getOptions();       
        $values = array();
        foreach ($manufacturerOptions as $manufacturerOption) { 
           //$manufacturerOption->getValue();  // Value
            $values[] = $manufacturerOption->getLabel();  // Label
        }
        return $values;
    }  
}

Call inside your phtml file,

<div class="manufacturer-name">
      <?php $getOptionValue = $this->getAllBrand();?>
      <?php foreach($getOptionValue as $value){ ?>
           <span><?php echo $value;?></span>
      <?php } ?>
</div>

Thanks.

4
  • This doesn't return the options for attributes configured to use swatch inputs, like color. The getOptions() method is hard coded to certain input types, like "dropdowns", so it skips the swatch input options. Just a heads up if anyone else runs in to that.
    – thaddeusmt
    Commented Aug 15, 2017 at 16:44
  • Hi @Rakesh, How I achieve this same but for Admin. I need these option value for Grid column filter. Can you please tell me.
    – Ravi Soni
    Commented Sep 20, 2017 at 10:20
  • How to get item attribute values based on store-level on order summary section at checkout Magento 2 Commented Mar 31, 2020 at 10:36
  • @Rakesh, does it work in magento 2.4 Commented Apr 8, 2021 at 7:23
4

Inject an instance of \Magento\Catalog\Model\Product\Attribute\Repository in your constructor (in a block, helper class or wherever):

/**
 * @var \Magento\Catalog\Model\Product\Attribute\Repository $_productAttributeRepository
 */
protected $_productAttributeRepository;

/**
 * ...
 * @param \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository
 * ...
 */
public function __construct(
    ...
    \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository,
    ...
) {
    ...
    $this->_productAttributeRepository = $productAttributeRepository;
    ...
}

Then create a method in your class to get the attribute by code:

/**
 * Get single product attribute data 
 *
 * @return Magento\Catalog\Api\Data\ProductAttributeInterface
 */
public function getProductAttributeByCode($code)
{
    $attribute = $this->_productAttributeRepository->get($code);
    return $attribute;
}

You can then call this method like so, e.g. inside a .phtml file

$attrTest = $block->getProductAttributeByCode('test');

Then you can make calls on the attribute object, e.g.

  1. Get options: $attribute->getOptions()
  2. Get frontend label for each store: $attrTest->getFrontendLabels()
  3. Debug the data array: echo '> ' . print_r($attrTest->debug(), true);

debug: Array ( [attribute_id] => 274 [entity_type_id] => 4 [attribute_code] => product_manual_download_label [backend_type] => varchar [frontend_input] => text [frontend_label] => Product Manual Download Label [is_required] => 0 [is_user_defined] => 1 [default_value] => Product Manual Download [is_unique] => 0 [is_global] => 0 [is_visible] => 1 [is_searchable] => 0 [is_filterable] => 0 [is_comparable] => 0 [is_visible_on_front] => 0 [is_html_allowed_on_front] => 1 [is_used_for_price_rules] => 0 [is_filterable_in_search] => 0 [used_in_product_listing] => 0 [used_for_sort_by] => 0 [is_visible_in_advanced_search] => 0 [position] => 0 [is_wysiwyg_enabled] => 0 [is_used_for_promo_rules] => 0 [is_required_in_admin_store] => 0 [is_used_in_grid] => 1 [is_visible_in_grid] => 1 [is_filterable_in_grid] => 1 [search_weight] => 1 )

1
  • 1
    This is a very well explained answer Commented Aug 8, 2018 at 21:27
0
   <?php
      /* to load the Product */
  $_product = $block->getProduct();
  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $attributeSet = $objectManager- 
   >create('Magento\Eav\Api\AttributeSetRepositoryInterface');
  $attributeSetRepository = $attributeSet->get($_product->getAttributeSetId());
  $_attributeValue  = $attributeSetRepository->getAttributeSetName();  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.