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
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 />';
}
}
you can get all the attributes as below:
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
$attribute->getCode();
}
Ref. https://magento.stackexchange.com/questions/98945/magento-2-how-can-i-get-all-product-attributes-and-get-the-value-yes-no
To get the groupId,
//groupCollection - Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory object
$groupCollection = $this->_groupCollection->create();
$groupCollection->addFieldToFilter('attribute_set_id',$attributeSetId); $groupCollection>addFieldToFilter('attribute_group_name','sample_group_name');
$firstItem = $groupCollection->getFirstItem();
echo $firstItem->getData('attribute_group_id');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With