1

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 = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->get('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $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.

1
  • Please check my answer and let me know if any issue. Oct 3, 2018 at 4:56

3 Answers 3

2

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 = \Magento\Framework\App\ObjectManager::getInstance();
$eavConfig = $objectManager->create('\Magento\Eav\Model\Config');
$attribute = $eavConfig->getAttribute('catalog_product', 'country');
$options = $attribute->getSource()->getAllOptions();

Using Injection

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

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

By Factory Method :

You need to inject \Magento\Eav\Model\Config in your construct

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', 'country');
$options = $attribute->getSource()->getAllOptions();

By Object Manager :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$eavConfig = $objectManager->get('\Magento\Eav\Model\Config');
$attribute = $eavConfig->getAttribute('catalog_product', 'country');

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

Note : Don't use object manager direct.

0
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $col = $_objectManager->create('\Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory');
    $cole = $_objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory');
    
    $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'];
                }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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