Magento 2 : How to get product attributes collection in custom module block file whose status is visible =>yes?

Magento 2 : How to get product attributes collection in custom module block file whose status is visible =>yes?

September 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 : How to get product attributes collection in custom module block file whose status is visible =>yes?

The Magento 2 platform is renowned for its robust e-commerce capabilities, but it can sometimes present challenges when trying to retrieve specific data like product attributes based on their visibility status. In this post, we’ll dive into how you can get the desired product attribute collection in a custom module block file that has a status of visible => yes.

Understanding the Current Implementation

The provided code snippet uses Magento’s EAV (Entity-Attribute-Value) system to fetch all attributes belonging to the catalog_product entity type. However, it does not filter these attributes based on their visibility status, which is causing the issue you’re encountering.

Let’s break down what the current getallattributes() function does:

  1. It initializes an empty array called $arr to store the attribute data.
  2. It fetches all the catalog_product entity type attributes by calling the _attributeFactory.
  3. It iterates through each attribute in the collection and retrieves essential information like the attribute ID and frontend label.

Filtering Attributes Based on Visibility Status

To filter the product attributes based on their visibility status, we need to modify the filter condition in the getallattributes() function. Currently, it only filters by entity type; we’ll add a condition to include only those attributes whose ‘is_visible’ field is set to 1 (which means ‘visible’).

Here’s how you can update your function:

public function getVisibleAttributes()
{
    $arr = [];
    $attributeInfo = $this->_attributeFactory->getCollection();
    $attributeInfo->addFieldToFilter(\Magento\Eav\Model\Entity\Attribute\Set::KEY_ENTITY_TYPE_ID, 4);
    $attributeInfo->addFieldToFilter('is_visible', 1);

    foreach($attributeInfo as $attributes)
    {
        // Your existing code
    }
    return $arr;
}

Key Changes Made

In the updated function:

  • We’ve added $attributeInfo->addFieldToFilter('is_visible', 1); to filter only those attributes where ‘is_visible’ is set to 1.
  • I’ve renamed the function from getallattributes() to getVisibleAttributes() for better clarity.

Retrieving Additional Attribute Data

If you need to retrieve additional data for each attribute, such as the input type or options, you can do so within the foreach loop. For example:

foreach($attributeInfo as $attributes)
{
    $attributeId = $attributes->getAttributeId();
    $frontendLabel = $attributes->getFrontendLabel();
    $inputType = $attributes->getFrontendInput();

    // Other attribute fields

    $arr[$attributeId] = [
        'label' => $frontendLabel,
        'type' => $inputType,
        // Add other field data here
    ];
}

Conclusion: Getting Visible Product Attributes in Magento 2

By following the updated code snippet, you can now fetch a collection of product attributes whose visibility status is set to visible from the admin panel. This approach uses Magento’s built-in EAV system and filter functions to ensure you get only the desired data.

Remember to replace any placeholders (like $arr in the example) with variables that fit your specific project context. If you need further assistance or want to explore more advanced Magento development techniques, feel free to reach out to our team at Laravel Company.

Happy coding!

Magento 2 Catalog Extensions Errors

Our network