TransWikia.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!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP