How to Get Child Configurable product Super attribute value Magento 2?

You can retrieve the Children item used super attribute label value data from the child and parent product SKU of the configurable product.

This article is useful when you are programmatically adding a Configurable product to the cart. 

Let’s take an example, You need to fetch used super attribute value of the child item, native Magento sample data configurable product name, Cassius Sparring Tank has product SKU MT12 and its child product name Cassius Sparring Tank-XL-Blue has product SKU MT12-XL-Blue.

Cassius Sparring Tank-XL-Blue Children product has Size Attribute value is XL and Color Attribute Value is Blue. We need to retrieve this value dynamically by the Parent and child product Id/SKU to add Dynamic Configurable product to the cart.

Size attribute id is 194 and XL option id value is 5606
Color attribute id is 93 and Blue option id value is 5486

<?php
namespace Jesadiya\SuperAttribute\Model;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Psr\Log\LoggerInterface;

class Data
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(
        ProductRepositoryInterface $productRepository,
        ProductAttributeRepositoryInterface $attributeRepository,
        LoggerInterface $logger
    ) {
        $this->productRepository = $productRepository;
        $this->attributeRepository = $attributeRepository;
        $this->logger = $logger;
    }

    /**
     * Get Super attribute details by the child and
     * parent sku value
     *
     * @param string $parentSku
     * @param string $childSku
     *
     * @return array
     */
    public function getChildSuperAttribute(string $parentSku, string $childSku )
    {
        $parentProduct = $this->getProduct($parentSku);
        $childProduct = $this->getProduct($childSku);
        $_attributes = $parentProduct->getTypeInstance(true)->getConfigurableAttributes($parentProduct);

        $attributesPair = [];
        foreach ($_attributes as $_attribute) {
            $attributeId = (int)$_attribute->getAttributeId();
            $attributeCode = $this->getAttributeCode($attributeId);
            $attributesPair[$attributeId] = (int)$childProduct->getData($attributeCode);
        }
        return $attributesPair;
    }

    /**
     * Get attribute code by attribute id
     * @param int $id
     * @return string
     * @throws NoSuchEntityException
     */
    public function getAttributeCode(int $id)
    {
        return $this->attributeRepository->get($id)->getAttributeCode();
    }

    /**
     * Get Product Object by SKU
     *
     * @param string $sku
     * @return ProductInterface|null
     */
    public function getProduct(string $sku)
    {
        $product = null;
        try {
            $product = $this->productRepository->get($sku);
        } catch (NoSuchEntityException $exception) {
            $this->logger->error(__($exception->getMessage()));
        }

        return $product;
    }
}

Call from the PHP class with a required argument to the function,

$parentSku = 'MT12';
$childSku = 'MT12-XL-Blue';
$superAttributeByChild = $this->getChildSuperAttribute($parentSku, $childSku);

Output:

Array
(
    [194] => 5606
    [93] => 5486
)