Get magento attribute options programmatically

This tutorial explains how to get magento attribute options programmatically. The following function get all attribute options in an array as show below.

Please also refer how to import attribute options from CSV programmatically and other magento tutorials.

import attribute options magento 2 programmatically
Get magento attribute options programmatically

function getExistingOptions( $object_Manager ) {
    
    $eavConfig = $object_Manager->get('\Magento\Eav\Model\Config');
    $attribute = $eavConfig->getAttribute('catalog_product', 'color');
    $options = $attribute->getSource()->getAllOptions();

    $optionsExists = array();
    
    foreach($options as $option) {
        $optionsExists[] = $option['label'];
    }
    
    return $optionsExists;

}

Share this Tutorial