Magento 2 How to get child item id of configurable product by super attribute details?

Let’s assume you know configurable product id and used child product super attribute details.
Example from Magento Sample data configurable product name Chaz Kangeroo Hoodie,  Configurable product id is 67.
Super attribute Array details,

[super_attribute] => Array
(
    [93] => 49
    [254] => 167
)

Using the bove details how we can get details of child item.

Refer below code snippet for Get child item id of a Configurable product by child’s Attribute details.

<?php
public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable
) {
    $this->productRepository = $productRepository;
    $this->configurable = $configurable;
}
/* Pass Configurable product id as $configId 
 * Pass array of super attribute of child item
 */
public function getChildFromProductAttribute($configId,$superAttribute) {
	$_configProduct = $this->productRepository->getById($configId);
	$usedChild = $this->configurable->getProductByAttributes($superAttribute ,$_configProduct);
	$childProductId = $usedChild->getId();
	return $childProductId;
}

Call function,

$configId = 67; //Configurable Product id
$superAttribute = Array(93 => 49,254 => 167); //childs super attribute details
$childId = $this->getChildFromProductAttribute($configId,$superAttribute);

Result is 52(Product name Chaz Kangeroo Hoodie-XS-Black). Using above way we got the child item data.

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.