How to get configurable attribute label and value in magento 2?

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load(**product id**);

$productTypeInstance = $objectManager->get('Magento\ConfigurableProduct\Model\Product\Type\Configurable');
$productAttributeOptions = $productTypeInstance->getConfigurableAttributesAsArray($product);
foreach ($productAttributeOptions as $key => $value) {

    $tmp_option = $value['values'];
    if(count($tmp_option) > 0)
    {
        echo "<h3>".$value['label']."</h3>";
        echo "<select id='".$key."_".$value['label']."'>";
        foreach ($tmp_option as $tmp) 
        {
            echo "<option value='".$key."_".$tmp['value_index']."'>".$tmp['label']."</option>";
        }
        echo "</select>";
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *