0

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.

  • On product page? – Vithal Bariya Jun 12 '18 at 6:11
  • No, In Custom Product list Page. – Saravanan DS Jun 12 '18 at 6:21
1

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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