3

How to get product attribute group in magento 2 from a attribute set. I want to show attribute on listing page by using a group so i can add more attributes in future

2 Answers 2

3

you simply get all product attribute by $product->getAttributes();

$productAttributes=$product->getAttributes();
        $group_id=9;
        $attributeSetId=4;
        foreach ($productAttributes as $attribute) {
            if ($attribute->isInGroup($attributeSetId, $group_id)) {
             echo $attribute->getFrontendLabel().' : '.$attribute->getFrontend()->getValue($product).'<br />';
            }

    }
5
  • @QaisarSqatti how can i identify the group id here any help or which table shoul i search fot it? Nov 19, 2016 at 6:37
  • Got it in eav_attribute_group table. but there are many entries for product details group. Nov 19, 2016 at 6:41
  • @NitinPawar <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productCollection = $objectManager->create('Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection'); $productCollection->addAttributeToSelect('*'); $productCollection->addFieldToFilter('attribute_set_id',4) ->addAttributeToSort('attribute_set_id', 'asc'); $productCollection->load(); foreach ($productCollection as $key => $value) { echo '<pre>'; print_r($value->getData()); } you can get group information from following collection Nov 21, 2016 at 5:50
  • I managed to get the group id from attribute set id and group name but i can't get the attribute from the group id. Please check my question magento.stackexchange.com/questions/147302/… Nov 24, 2016 at 9:58
  • @Qaisar Satti , I have put this code to grouped.phtml file but not working. Oct 12, 2017 at 13:38
1

We can use getAttributeGroupId method:

vendor/magento/module-catalog/Model/Config.php

public function getAttributeGroupId($attributeSetId, $name)
{
   ......     
}

We can get attribute group id:

   $obj = \Magento\Framework\App\ObjectManager::getInstance();

    /** @var \Magento\Catalog\Model\Config $config */
    $config= $obj->get('Magento\Catalog\Model\Config');

    $attributeGroupId = $config->getAttributeGroupId(1, 'General');

Should take a look eav_attribute_group.

Your Answer

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

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