How to get value of super attribute?

How to get value of super attribute?

July 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get value of super attribute?

Accessing Super Attribute Option Value in Magento

As a senior developer working extensively with e-commerce platforms like Magento, I often encounter questions about retrieving specific attribute values for configurable products. Today, let’s dive into how to efficiently get the value of a super attribute option for a product.

The Scenario: Retrieving Super Attribute Option Values

You have a product that has a super attribute (let’s call it ‘Color’) with multiple options (‘Red’, ‘Blue’, etc.). Each color option has its own price or other distinct values. Your goal is to retrieve the specific value associated with the selected option for a particular product.

The Problem: Inefficient Code Generation

The provided code snippet, while functional, demonstrates some common pitfalls in Magento development:

  1. Using deprecated methods ($produto_cor = Mage::getModel('catalog/product')->load($id_produto);)
  2. Poor code organization and naming conventions
  3. No error handling or validation of input data

The Solution: Efficient Super Attribute Retrieval

Let’s refactor the code to meet Magento best practices:

<?php
require_once 'app/Mage.php';
Mage::init();

// Load the product by ID
$product = Mage::getModel('catalog/product')->load($id_produto);

// Check if the product exists
if (!$product->getId()) {
    echo "Product not found.";
    return;
}

// Get the configurable attributes array
$attributes = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);

// Iterate through each attribute
foreach ($attributes as $attribute) {

    // Fetch all options for this super attribute
    $options = $attribute['values'];

    // Iterate through each option
    foreach ($options as $option) {
        echo "Option ID: {$option['option_id']} - Price Value: {$option['pricing_value']} <br />";
    }
}
?>

Key Improvements in the Refactored Code

  1. We start by initializing Magento properly to ensure the application context is set correctly.
  2. We validate that the product exists before proceeding with attribute retrieval to avoid potential errors.
  3. The code is more organized, using clear variable names and comments for better readability.
  4. We directly access the pricing value from the option array without unnecessary nested loops.

Retrieving Super Attribute Options by Selected Value

If you want to retrieve the specific option selected for a product, you can modify the code further:

// Get the super attribute ID (assuming it's 'color')
$super_attribute_id = 91;

// Fetch the selected options for this product
$selected_options = $product->getTypeInstance(true)->getSelectedConfigurableOptions($product);

// Iterate through the selected options
foreach ($selected_options as $option) {
    if ($option['attribute_id'] == $super_attribute_id) {
        // This is our super attribute option

        echo "Option ID: {$option['value_index']} - Price Value: {$option['price']} <br />";
        break;
    }
}

Conclusion

Retrieving the value of a super attribute option in Magento requires understanding the product configuration and its attributes. By following best practices, you can create efficient and maintainable code to handle these tasks.

Remember to:

  1. Always validate your input data.
  2. Use proper error handling mechanisms.
  3. Follow Magento’s naming conventions for better integration with core functionality.

For more advanced customizations or if you’re looking to streamline your e-commerce development process, consider exploring Laravel Company, where we specialize in providing tailored solutions and expert guidance for your Magento projects.

Catalog Configuration Pricing Errors

Our network