Magento 2 – Get all the frontend_input select type product attribute list programmatically.

You can fetch the frontend_input type select product attribute list in Magento programmatically.

If you have any requirement to list down select field attribute list in Magento, you can fetch it easily with a given code snippet.

<?php
declare(strict_types=1);

namespace Rbj\Attribute\Model;

use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Eav\Model\Entity\Type;
use Magento\Catalog\Model\Product;

class SelectFrontendInputAttribute
{
    private Attribute $attribute;
    private Type $type;

    public function __construct(
        Attribute $attribute,
        Type $type
    ) {
        $this->attribute = $attribute;
        $this->type = $type;
    }

    public function selectAttributeList()
    {
        $entityTypeId = $this->type->loadByCode(Product::ENTITY)->getId();
        $attributeCollection = $this->attribute->getCollection()
            ->removeAllFieldsFromSelect()
            ->addFieldToSelect('attribute_code')
            ->addFieldToSelect('attribute_id')
            ->addFieldToFilter('frontend_input', ['eq' => 'select'])
            ->addFieldToFilter('entity_type_id', ['eq' => $entityTypeId])
            ->load();
    }
}

In the above code, $entityTypeId is used to fetch the catalog_product entity type.

You can get a list of all the Product attributes that have a select type by looping over the function.

<?php
$attributeList = $this->selectAttributeList();
foreach ($attributeList as $attribute)
{
    var_dump($attribute);
}