How to get Product id using Super attribute in Magento?

How to get Product id using Super attribute in Magento?

July 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get Product id using Super attribute in Magento?

Getting the Actual Product ID from Super Attribute in Magento

As a senior developer working on e-commerce projects, I often encounter scenarios where we need to retrieve specific product attributes or identifiers from complex configurations like configurable products in Magento. In your case, you’re dealing with a configurable product that has two simple products as its sizes (Small and Medium). When the user selects an option and adds it to the cart, the super attribute is posted instead of the actual product ID.

The super_attribute array contains information about the selected attributes for the configurable product. In your scenario:

[super_attribute] => Array
    (
        [129] => 128
    )

Here,

  • 129 is the attribute ID (Size)
  • 128 is the attribute value (Small)

To get the actual product ID of size “Small”, you can use Magento’s core functionality. Here’s a step-by-step guide to achieve this:

Step 1: Retrieve the Configurable Product

First, fetch the configurable product using its model or SKU.

$product = $this->_objectManager->create('Magento\Catalog\Model\Product')->loadByAttribute('sku', 'your_sku');

Replace 'your_sku' with the actual SKU of your configurable product.

Step 2: Get Associated Simple Products

The configurable product has a collection of associated simple products. Retrieve this collection:

$associatedProducts = $product->getTypeInstance()->getUsedProducts($product);

Step 3: Find the Matching Simple Product

Loop through the collection to find the simple product that matches your super_attribute:

foreach ($associatedProducts as $simpleProduct) {
    if ((int)$simpleProduct->getData('attribute_set_id') == (int)$productData['super_attribute'][129]) {
        // Found the matching simple product
        break;
    }
}

Step 4: Retrieve the Simple Product ID

Finally, you can access the actual product ID of the size “Small”:

$simpleProductId = $simpleProduct->getId();

Now, $simpleProductId contains the ID of the simple product associated with the selected attribute value (128).

Best Practices and Tips

  1. Always use Magento’s core models and APIs for retrieving products to ensure data consistency.

  2. If you’re working with AJAX requests, make sure to handle security properly, especially when dealing with product data.

  3. In your controller or observer logic, you can now use the $simpleProductId to perform actions specific to that size (e.g., apply discounts, set custom options).

Conclusion

Retrieving the actual product ID from a super attribute in Magento requires understanding how configurable products work and leveraging the available core functionalities.

By following this guide, you should be able to successfully get the correct product ID for the selected size or attribute value when working with AJAX modules or any other scenario involving configurable products in your Magento project.

For more advanced techniques and best practices on working with Magento’s configuration system, consider exploring resources from Laravel Company. They provide comprehensive guides and tutorials to help you build robust e-commerce solutions.

Checkout Catalog Configuration

Our network