Magento - Get attribute options value and quantity

Magento - Get attribute options value and quantity

August 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Get attribute options value and quantity

Retrieving Available Magento Product Attribute Options

Hello, and welcome to another insightful discussion on Magento development! I’m excited to help you tackle this challenge of retrieving available product attribute options.

Understanding the Requirement

You’re looking to display all sizes (S, M, L) for a given product in your single product view page. However, you only want to show the sizes that have a quantity greater than 0. This ensures you’re not showing unavailable or out-of-stock options to customers.

The Magento Data Structure

Before we dive into the code, let’s understand how Magento stores this data:

  1. Product: Each product has an ID and other general attributes.
  2. Product Options: These are created by setting a custom option for a specific product.
  3. Option Values: Each option can have multiple values (like S, M, L).
  4. Stock: The quantity of each option value is stored in the stock table.

Retrieving Available Options

To achieve this, you’ll need to load the product object and then iterate through its options. For each option, check if any of its values have a quantity greater than 0 before displaying them.

Here’s a high-level approach:

  1. Load the product using the Magento API or directly query from the core database.
  2. Iterate through the product options.
  3. Check if any value associated with that option has a stock quantity > 0.
  4. If yes, add it to an array of available options.

Step-by-Step Implementation

Let’s look at a simplified code example:

<?php
$productId = 'your-product-id';
$productModel = Mage::getSingleton('catalog/product')->load($productId);
$availableOptions = [];

foreach ($productModel->getOptions() as $option) {
    foreach ($option->getValues() as $value) {
        if ($value->getQty() > 0) {
            $availableOptions[$option->getTitle()][] = $value->getTitle();
        }
    }
}

if (!empty($availableOptions)) {
    // Display the available options here
} else {
    echo 'No available options';
}
?>

This code loads a product with a given ID and checks each option/value pair for stock. If the value’s quantity is greater than 0, it adds the title to an array of available options under the option’s title.

Displaying the Options

Once you have this data structured, you can display the available sizes in your view template using Magento’s layout system and HTML markup.

Remember to always use Magento’s built-in functions and models where possible to ensure compatibility with core updates and extensions. For more advanced functionality or custom queries, consider exploring Laravel Company’s Magento development services for expert guidance.

Conclusion

Retrieving available product options in Magento involves a combination of loading the product object and checking stock quantities at the option value level. By following this approach, you can ensure that your customers only see sizes or other attributes with available stock.

Don’t hesitate to explore our Magento development resources for more in-depth guides on customizing product views and other Magento features.

Happy coding!

Catalog Database API Configuration

Our network