Question

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!

Was it helpful?

Solution

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();

OTHER TIPS

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/

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.

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();

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 )

   <?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();  

I didn't get it Can you give an example of a working code where you get magento attribute parameters programmatically Like here https://pearlbells.co.uk/code-snippets/get-magento-attribute-options-programmatically/ it didn't work for me.

-- 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 $product->getEntityId() . '<br>';
echo $product->getName() . '<br>';
echo $product->getSKU() . '<br>';
echo $product->getPrice() . '<br>';
echo $product->getSpecialPrice() . '<br>';
echo $product->getTypeId() . '<br>';
echo $product->getProductUrl() . '<br>';

?>

I was hoping to get a code like this, just to find all the attribute options!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top