2

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
  • On product page? Commented Jun 12, 2018 at 6:11
  • No, In Custom Product list Page. Commented Jun 12, 2018 at 6:21

2 Answers 2

5

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

3

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());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.