How can I get all the available colors of a single product in Magento 2?

  • Is this simple product a configurable product or simple product? color is a dropdown attribute or multi-select attribute? – Amit Bera Aug 13 '18 at 11:32
  • It is a configurable product – Borche Glafche Aug 13 '18 at 11:44

Assume that you have configurable product object:

Then first you have to get it all child products ids using

$childrenIds = $configurableProduct->getTypeInstance()->getChildrenIds($configurableProduct->getId());

Then create a product collection and filter that collection by$childrenIds

 //Assume that your color attribute code is 'color'
// optimized the collection

foreach($productCollectionFactory as $product){
    echo  $product->getData('color');

Here $productCollectionFactory is object of \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory

It's working fine.

  • Use setPageSize and setCurPage need to use for optimized the collection – Amit Bera Aug 14 '18 at 14:21

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.