Magento 2 - How to get attribute options value of eav entity?
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();
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.