2

Hi I want to fetch the attributes of a specific group only. How can i filter the attributes.

Code:

$attrCodes = $block->getProductAttrData($pattr);
foreach($attrCodes as $attrCode)
{
    echo $attrCode->getDefaultFrontendLabel().'<br>';
    echo $attrCode->getAttributeCode().'<br>';
}
}

I want to filter the product details group attributes. How can i achieve this.

4
  protected $_groupCollection; 


  public function __construct(
        ......
        \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $_groupCollection
        .....

    ) {


        .....
        $this->_groupCollection = $_groupCollection;
        .....
    }
    .
    public function getAttributeGroupId($attributeSetId)
    {
         $groupCollection = $this->_groupCollection->create();
         $groupCollection->addFieldToFilter('attribute_set_id',$attributeSetId);
         $groupCollection->addFieldToFilter('attribute_group_name','Testing'); // here you can add your attribute code 
         return $groupCollection->getFirstItem(); 

    }

now in phtml file

    $groupid=$block->getAttributeGroupId($product->getAttributeSetId());
    $productAttributes=$product->getAttributes();
      foreach ($productAttributes as $attribute):
        if ($attribute->isInGroup($product->getAttributeSetId(), $groupid->getAttributeGroupId())):
            if($attribute->getFrontend()->getValue($product)):
                echo $attribute->getFrontendLabel().' : '.$attribute->getFrontend()->getValue($product).'<br />';
            endif;
         endif;
      endforeach; 
| improve this answer | |

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.