Magento 2 – How to get all options of specific attribute type?

In Magento 2 we can get all options of specific EAV entity types attribute. In Magento 2 There are many native EAV Entity Types are available.

You can get all options of specific attribute, Let’s we take a catalog_product Entity Type.
We take Color attribute from Catalog_Product Eav Entity Types. Fetch all the options of color attribute.
Create Block file,

<?php
namespace Rbj\AttributeOption\Block

class Option extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Eav\Model\Config $eavConfig,
        array $data = []
    ) {
        $this->eavConfig = $eavConfig;
        parent::__construct($context, $data);
    }

    /**
     * Entity Type Catalog
     * Attribute code color
     * @return array
     */
    public function getOptionList()
    {
        $attribute = $this->eavConfig->getAttribute('catalog_product', 'color');
        return $attribute->getSource()->getAllOptions();
    }
}

Call Function From Template file,

$result = $block->getOptionList();
foreach($result as $option) {
    echo $option['value']. '=>' .$option['label'];echo "<br />";
}
echo "<pre>";print_r($result);

The result looks like,

Array
(
    [0] => Array
        (
            [label] =>
            [value] =>
        )

    [1] => Array
        (
            [value] => 49
            [label] => Black
        )

    [2] => Array
        (
            [value] => 50
            [label] => Blue
        )

    [3] => Array
        (
            [value] => 51
            [label] => Brown
        )

    [4] => Array
        (
            [value] => 52
            [label] => Gray
        )
        ....

 

Magento 2 Get Attribute Swatch color hashcode by option id

We can get hash code value of specific visual swatch by swatch option id.
We have created Color Product attribute from Stores -> Attributes -> Product.
Color attribute code with Catalog Input Type for Store Owner set to Visual swatch.

Now We can get the color hash code by color option id using below code snippet.

public function __construct(
    \Magento\Swatches\Helper\Data $swatchHelper,
) {
    $this->swatchHelper = $swatchHelper;
}
/* Get Hashcode of Visual swatch by option id */
public function getAtributeSwatchHashcode($optionid) {
    $hashcodeData = $this->swatchHelper->getSwatchesByOptionsId([$optionid]);
    return $hashcodeData[$optionid]['value'];
}

Call getAtributeSwatchHashcode() function from template,

$hashcode = $this->getAtributeSwatchHashcode(50); //assume 50 is Red option id

The result will be like color Red hashcode is #FF0000

How to get configurable product’s used super_attribute details programmatically in Magento 2?

We can get the details of the used super attribute value for the configurable product. You will get the attribute value used in the configurable product using the below example.

Let’s assume a configurable product generated with color and size attributes. But how we can get details of super attribute value using a programmatic way that product is made using color or size or color and size or any other attributes combination.

You can retrieve the Child Product Used Super attribute details for the configurable product.

We can get Dynamically Configurable Product’s used Super attribute details by below code snippet.

<?php
namespace Rbj\ConfigProduct\Block;

class SuperAttribute
{
    public function __construct(
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;
    }

    /**
     * get Super Attribute details by configurable product id
     */
    public function getSuperAttributeData($productId);
    {
        /** @var \Magento\Catalog\Model\Product $product */
        $product = $this->productRepository->getById($id);
        if ($product->getTypeId() != \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
            return [];
        }

        /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $productTypeInstance */
        $productTypeInstance = $product->getTypeInstance();
        $productTypeInstance->setStoreFilter($product->getStoreId(), $product);

        $attributes = $productTypeInstance->getConfigurableAttributes($product);
        $superAttributeList = [];
        foreach($attributes as $_attribute){
            $attributeCode = $_attribute->getProductAttribute()->getAttributeCode();;
            $superAttributeList[$_attribute->getAttributeId()] = $attributeCode;
        }
        return $superAttributeList;
    }

Call function from a PHP class file,

$productId = 67;
$superAttribute = $this->getSuperAttributeData($productId);
echo "<pre>";print_r($superAttribute);

The result will be,

Array
(
   [93] => color
   [254] => size
)

Where 93,254 are attribute id and color, size is attribute code. Based on output we can say a configurable product is made using color and size super attribute.