How to Get Product Attribute List using Attribute Set ID in Magento 2
<?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());
}
Comments
Post a Comment