In Magento2, How can get the all (product) Attributes based on the Attribute Set Id or Attribute Set Name in the Custom Product List Page.
2 Answers
you can make use of the Magento\Catalog\Api\ProductAttributeManagementInterface
$attributeSetId = 20;//your_attributeSetId
$productAttributesManagement = $this->objectManager->create('Magento\Catalog\Api\ProductAttributeManagementInterface');
$productAttributes = $productAttributesManagement->getAttributes($attributeSetId);
Please note that you should not be using the object manager directly. This is just for demonstration
Here is the Code:
$attributeSetId = 20;//your_attributeSetId
$attributeSet = $this->objectManager->create('Magento\Eav\Api\AttributeSetRepositoryInterface');
$attributeSetRepository = $attributeSet->get($attributeSetId);
$attribute_set_name = $attributeSetRepository->getAttributeSetName();
$attributeGroupCollection = $this->objectManager->get ( 'Magento\Eav\Model\Entity\Attribute\Group' )->getCollection ();
$attributeGroupCollection->addFieldToFilter ( 'attribute_group_name', $attribute_set_name );
$attributeGroupCollection->addFieldToFilter ( 'attribute_set_id', $attributeSetId );
$attributeGroupId = '';
foreach ( $attributeGroupCollection as $attributeGroup ) {
$attributeGroupId = $attributeGroup->getId ();
break;
}
$attributeCollection = $this->objectManager->get ( 'Magento\Eav\Model\Entity\Attribute' )->getCollection ();
$attributeCollection->setAttributeSetFilter ( $attributeSetId );
$attributeCollection->setAttributeGroupFilter ( $attributeGroupId );
echo "<pre>";
print_r($attributeCollection->getData());