Magento 2 get attribute values

Magento 2 get attribute values

July 5, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 get attribute values

Retrieving Magento 2 Attribute Options

When working with product attributes in Magento 2, you might need to pull all available options for an attribute across your store’s entire catalog. This is particularly useful when building dropdown select menus or managing options programmatically.

Understanding the New Architecture

Magento 2 introduced significant changes to the core architecture compared to its predecessor. The Mage class was deprecated in favor of the \Magento\ namespace, and many models were refactored using object-manager instantiation instead of direct singleton access.

Retrieving Attribute Options in Magento 2

To get all options for a specific attribute in Magento 2, follow these steps:

  1. Get the Entity Manager: First, you need to retrieve an instance of the entity manager responsible for handling product attributes.
$entityManager = $objectManager->get('\Magento\Eav\Model\EntityManager');
  1. Load the Attribute Model: Use the entity manager to load the attribute model by its code or ID.
$attribute = $entityManager->getAttribute(\Magento\Framework\App\Request\Http::class, 'color');
  1. Load the Option Collection: Magento 2 provides a dedicated collection for attribute options. Load this collection and filter it using the loaded attribute.
$optionCollection = $attribute->getSource()->getAllOptions(false);
  1. Process the Options: The $optionCollection is an array of options where each option is represented as an associative array with keys like label, value, etc.

Example: Displaying All Color Options

Let’s say you want to display all color options in your store’s catalog:

$entityManager = $objectManager->get('\Magento\Eav\Model\EntityManager');
$attribute = $entityManager->getAttribute(\Magento\Framework\App\Request\Http::class, 'color');

$optionCollection = $attribute->getSource()->getAllOptions(false);

foreach ($optionCollection as $option) {
    echo $option['label'] . "\n";
}

This script will print all available color options in your Magento 2 store.

Conclusion

Retrieving attribute options in Magento 2 is a straightforward process once you understand the new object-manager instantiation and model loading mechanisms. The getSource()->getAllOptions() method provides an efficient way to pull all options for a given attribute without needing to filter by product or use complex queries.

Remember, while developing custom extensions or themes, it’s crucial to avoid direct access to protected methods like $objectManager->get(). Instead, consider using dependency injection and the Magento 2 plugin system. For more advanced topics and examples, feel free to explore our other tutorials on Magento 2 development best practices.

Happy coding!

Magento 2 Catalog

Our network