How To

How to Get Product Attribute List using Attribute Set ID in Magento 2

Hello Magento Friends,

In today’s tutorial blog, we will learn How to Get Product Attribute List using Attribute Set ID in Magento 2.

Product attributes are the characteristics of a product. A product attributes set can be defined as a list of attributes assigned to a particular product family. You can easily create new attribute sets in Magento 2 based on your requirement.

When you need to retrieve the attribute list added to a particular attribute set, you can do this with the help of the attribute set ID in Magento 2.

Let’s see How to Get the Product Attribute List using Attribute Set ID in Magento 2.

Steps to Get Product Attribute List using Attribute Set ID in Magento 2:

Step 1:  We are getting data in the root script. So the path of the file will be as follows

magento_root_directory\pub\getAttribute.php

Then add the code as given below

<?php

use \Magento\Framework\AppInterface;

try
{
 require __DIR__ . '/../app/bootstrap.php'; 
}
catch (\Exception $e)
{
 echo 'Autoload error: ' . $e->getMessage();
 exit(1);
}
try
{
 $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

 $objectManager = $bootstrap->getObjectManager();

 $appState = $objectManager->get('\Magento\Framework\App\State');
 $appState->setAreaCode('frontend');
 
 $productAttributesManagement = $objectManager->get('\Magento\Catalog\Api\ProductAttributeManagementInterface');
 $productAttributesRepository = $objectManager->get('\Magento\Catalog\Api\ProductAttributeRepositoryInterface');
 $attrubuteOptionRepository = $objectManager->get('\Magento\Catalog\Model\Product\Attribute\Repository');

 $productAttributes = $productAttributesManagement->getAttributes(9); // enter attribute id
 $attributeData = array();
 foreach($productAttributes as $key=>$value)
        {        
     $attributeData[$key]['label']=$productAttributesRepository->get($key)->getDefaultFrontendLabel();
     $attributeData[$key]['code']=$productAttributesRepository->get($key)->getAttributeCode();
     $attributeData[$key]['type']=$productAttributesRepository->get($key)->getFrontendInput();
     $attributeOptions = $attrubuteOptionRepository->get($productAttributesRepository->get($key)->getAttributeCode())->getOptions();
     $option = array();
     foreach($attributeOptions as $value)
            {
  $option = $value->getData();
     }
     if(count($option))
            {
  $attributeData[$key]['option']=$option;
     }           
 }
 echo "<pre>";
 print_r($attributeData);
}
catch(\Exception $e)
{
 print_r($e->getMessage());
}

Conclusion:

Using the above method, you can quickly obtain a product attribute list using the attribute set ID in Magento 2. If you have any doubt, let me know through the comment section. Share the tutorial with your friends and stay updated on the latest Magento 2 guides.

Happy Coding!

Click to rate this post!
[Total: 3 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

6 hours ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

8 hours ago

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

2 days ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

5 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 week ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 week ago