AnswerBun.com

Magento 2 Get all attributes option in phtml

Magento Asked by user1799722 on November 29, 2020

I have created an attribute with multiple options I have to get all option of in dropdown.

Attribute created: country
Options: India, USA, UK

below is my code in phmtl file

$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$productCollection = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
    /** @var MagentoCatalogModelResourceModelProductCollection $collection */
    $collection = $productCollection->create();
    $collection->addAttributeToSelect('country')
        ->addAttributeToFilter('country', array('notnull' => true))// get only not null values
        ->getSelect()->group('country');
print_r($collection);

it does not show all attribute option of the country.

3 Answers

$_objectManager = MagentoFrameworkAppObjectManager::getInstance();
    $col = $_objectManager->create('MagentoEavModelResourceModelEntityAttributeGroupCollectionFactory');
    $cole = $_objectManager->create('MagentoCatalogModelResourceModelProductAttributeCollectionFactory');
    
    $attributeSetId = 4; //Here is attribute set id
    $groupIds = [];
    $attributeids = [];
    $somearray=[];
        $groupCollection = $col->create()
        ->setAttributeSetFilter($attributeSetId)
        ->load(); // product attribute group collection
    foreach ($groupCollection as $group) {
        
        $groupAttributesCollection = $cole->create()
            ->setAttributeGroupFilter($group->getId())
            ->addVisibleFilter()
            ->addFieldToFilter('is_user_defined', array('eq' => 1))
                ->load(); // product attribute collection
       
        foreach ($groupAttributesCollection->getItems() as $attribute) {
            
            $opte=$attribute->getname();
            //echo "<strong>".$opte."</strong>";
            $opt=$attribute->getid();
            $options=$block->getattributeoptions($opt);
    
                  foreach($options as $values){
                  // echo $values['label'];
                }
        }
    }

Answered by Anees on November 29, 2020

By Factory Method :

You need to inject MagentoEavModelConfig in your construct

protected $eavConfig;
public function __construct(
    ...
    MagentoEavModelConfig $eavConfig,
    ...
){
    ...
    $this->eavConfig = $eavConfig;
    ...
}

then you can use that in your class

$attribute = $this->eavConfig->getAttribute('catalog_product', 'country');
$options = $attribute->getSource()->getAllOptions();

By Object Manager :

$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$eavConfig = $objectManager->get('MagentoEavModelConfig');
$attribute = $eavConfig->getAttribute('catalog_product', 'country');

$options = $attribute->getSource()->getAllOptions(); // for attribute option.

Note : Don't use object manager direct.

Answered by Rohan Hapani on November 29, 2020

The code which you have used only give those options which are selected on all products.'

You want to all option of a product attribute then you have to use below code:

Using OBject Manager:

$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$eavConfig = $objectManager->create('MagentoEavModelConfig');
$attribute = $eavConfig->getAttribute('catalog_product', 'country');
$options = $attribute->getSource()->getAllOptions();

Using Injection

protected $eavConfig;
public function __construct(
    ...
    MagentoEavModelConfig $eavConfig,
    ...
){
    ...
    $this->eavConfig = $eavConfig;
    ...
}

public function getAllOption()
{
    attribute = $this->eavConfig->getAttribute('catalog_product', 'country');
    $options = $attribute->getSource()->getAllOptions();
}

Answered by Amit Bera on November 29, 2020

Add your own answers!

Related Questions

Magento 2 : create view table via setup script

2  Asked on February 15, 2021 by dominic-xigen

   

Get userid of the admin logged in magento 2 helper

1  Asked on February 12, 2021 by fahad-bashir

   

Product saving problem in admin control panel

1  Asked on February 12, 2021 by venkatesha

 

Print Address / Shipping Label For Magento 2

0  Asked on February 12, 2021 by naveen-sama

     

are the two codes same?

1  Asked on February 11, 2021 by hong-thng

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir