Site icon MageComp Blog

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

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!

Exit mobile version