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? Jun 12 '18 at 6:11
  • No, In Custom Product list Page. Jun 12 '18 at 6:21
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, privacy policy and cookie policy

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