To use Object Manager is not a proper way but this code is for testing purpose only.

I want to get attribute value which is available in configurable products. I have tried with below code, but I get an all Attribute value which is assigned in the attributes drop-down list.

Please See below code

    $objectManager  = \Magento\Framework\App\ObjectManager::getInstance();
    $repository = $objectManager->create('Magento\Catalog\Model\ProductRepository');
    $product    = $repository->getById($p_coll->getId());    
    $data = $product->getTypeInstance()->getConfigurableOptions($product);
    $options = array();

    foreach ($data as $attr) {
        foreach ($attr as $p) {
            $options[] = $p['attribute_code'];

    $statu = 0;

    $eavConfig = $objectManager->get('\Magento\Eav\Model\Config');
    foreach (array_unique($options) as $code) {
        $attribute     = $eavConfig->getAttribute('catalog_product', $code);
        $options       = $attribute->getSource()->getAllOptions();
        $optionsExists = array();
        if ($options) {
            echo $code;
            echo "<select>";
            foreach ($options as $option) {
                    //$optionsExists[] = $option['label']; 
                if ($option['label']) {
                    echo "<option value='" . $option['label'] . "'>" . $option['label'] . "</option>";
            echo "</select>";


Please see below screen shot

enter image description here

There are only 2 colours available for this configurable product, but all colour options show in my collection. Please see below screenshot. enter image description here


Please try below code and please enter product id to get the value


use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
// adding bootstrap
$bootstraps = Bootstrap::create(BP, $_SERVER);
$object_Manager = $bootstraps->getObjectManager();

$app_state = $object_Manager->get('\Magento\Framework\App\State');

$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>";
| improve this answer | |
  • Could you please tell me how to get selected attribute value ? – Pratik Mehta Nov 30 '18 at 6:18
public function execute()
    $collection = $this->_collectionFactory->create()

    foreach($collection as $attributes)
        $attributeId = $attributes->getAttributeId();
        echo $attributeId."<br/>";
        $attribute =$this->eavConfig->getAttribute('catalog_product', $attributeId );//"color" is the attribute_code
        $allOptions = $attribute->getSource()->getAllOptions(true, true);
            foreach ( $allOptions as $instance ) {
                $id = $instance['value']; //id of the option
                echo $id ;
                $value = $instance['label']; //Label of the option
                echo $value;
    return $this->_pageFactory->create();

This will give all configurable product attribute .place this code wherever you want!!!!! This will give you attribute id and all its value

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.