Magento2 – How to Get Attribute Group ID Using Attribute Set ID

attribute-setattributesmagento2

I need to get all the attribute group id associated with the product.

Code:

$product_coll = $block->getProduct($product_id);
$pattr = $product_coll->getAttributeSetId();
$attrCodes = $block->getProductAttrData($pattr);
foreach($attrCodes as $attrCode)
{
    echo $attrCode->getDefaultFrontendLabel().'<br>';
    $code = $attrCode->getAttributeCode().'<br>';
    echo $product_coll->getData($code).'<br>';
    echo $block->getProductAttrValue($product_id, $code).'<br>';

}

I want to filter the attribute with it's group or group name "product details".
Any help appreciated.

Best Answer

Block function

public function getAttributeGroupId($attributeSetId)
    {
         $obj = \Magento\Framework\App\ObjectManager::getInstance();

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

        return $attributeGroupId = $config->getAttributeGroupId($attributeSetId, 'Product Details');

    } 

In html file

$group_id = $block->getAttributeGroupId('attribute_set_id');