How to Get Configurable Attribute for any Configurable Product in Magento 2.x

We often get all child attributes of any configurable product instead of getting only configurable attribute (like Size, Color etc). So if we want to show all the variation of product on the search page, we can get array of configurable attribute using below code..


public function getChildConfigurableAttribute($product)
{

$data = $product->getTypeInstance()->getConfigurableOptions($product);
$options = array();
$attributes = [];
foreach($data as $attributes){
foreach($attributes as $atrribute) {
$configurableAttributes[$atrribute['attribute_code']][] = $atrribute['option_title'];
}

return $configurableAttributes;
}

Output (is like):

 [color] Array ( 
 [0]=>red
 [1]=>blue
 [2]=>green
 )
 [size]  Array( 
 [0]=>S
 [1]=>M
 [2]=>L
 [3]=>XL
 ) 

Happy Coding 🙂

Related Post

Leave a Reply

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