Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get product attribute by product attribute group in magento 2

Tags:

magento2

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

like image 993
Waqar Avatar asked Sep 01 '16 05:09

Waqar


3 Answers

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 />';
            }

    }
like image 66
Qaisar Satti Avatar answered Oct 14 '22 16:10

Qaisar Satti


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

like image 31
Sarfaraz bheda Avatar answered Oct 14 '22 15:10

Sarfaraz bheda


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');
like image 35
Gopalakrishnan Srinivasan Avatar answered Oct 14 '22 14:10

Gopalakrishnan Srinivasan