Im currently invoking the method getAttributeGroupId using the objectmanager, however the object manager should not be used to instantiate as mentioned in the Magento 2 documentation. What is the best practice way to instantiate? Please help ive been stuck on this for dayz.
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$config= $_objectManager->get('Magento\Catalog\Model\Config');
$attributeGroupId = $config->getAttributeGroupId($_product->getAttributeSetId(), '');
Use following code
$groupCollection = $_objectManager->create('Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection');
$groupCollection->addFieldToFilter('attribute_set_id',$_product->getAttributeSetId());
$groupCollection->setOrder('sort_order','ASC');
echo $groupCollection->getId();
You are using the objectmanager to instantiate. This is not good practice according to magento 2 documentation. Please provide a best practice solution.
Hello
You need to create custom block like this
<?php
namespace QaisarSatti\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
protected $_productloader;
protected $request;
protected $_groupCollection;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ProductFactory $_productloader,
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $_groupCollection,
\Magento\Framework\App\Request\Http $request
) {
$this->_productloader = $_productloader;
$this->request = $request;
$this->_groupCollection = $_groupCollection;
parent::__construct($context);
}
//get the product
public function getLoadProduct()
{
$id=10;
return $this->_productloader->create()->load($id);
}
//Get attribute group id
public function getAttributeGroupId($attributeSetId)
{
$groupCollection = $this->_groupCollection->create();
$groupCollection->addFieldToFilter('attribute_set_id',$attributeSetId);
$groupCollection->addFieldToFilter('attribute_group_name','Grid Attributes');
return $groupCollection->getFirstItem();
}
//Get all attribute groups
public function getAttributeGroups($attributeSetId)
{
$groupCollection = $this->_groupCollection->create();
$groupCollection->addFieldToFilter('attribute_set_id',$attributeSetId);
$groupCollection->setOrder('sort_order','ASC');
return $groupCollection;
}
//get attribute by groups
public function getGroupAttributes($pro,$groupId, $productAttributes){
$data=[];
$no =__('No');
foreach ($productAttributes as $attribute){
if ($attribute->isInGroup($pro->getAttributeSetId(), $groupId) && $attribute->getIsVisibleOnFront() ){
if($attribute->getFrontend()->getValue($pro) && $attribute->getFrontend()->getValue($pro)!='' && $attribute->getFrontend()->getValue($pro)!=$no){
$data[]=$attribute;
}
}
}
return $data;
}
}
Template files likes this
$_product=$block->getLoadProduct();
$groupid=$block->getAttributeGroupId($_product->getAttributeSetId());
$attributesgroups=$block->getAttributeGroups($_product->getAttributeSetId());
$productAttributes=$_product->getAttributes();
?>
<?php
foreach ($attributesgroups as $attributesgroup):
$attributes=$block->getGroupAttributes($product,$attributesgroup->getAttributeGroupId(),$productAttributes);
if($attributes){ ?>
<h3 class="col label" scope="row"><?php echo $attributesgroup->getAttributeGroupName() ?></h3>
<div class="additional-attributes-wrapper table-wrapper block">
<table class="data table additional-attributes" id="product-attribute-specs-table">
<tbody>
<?php
foreach ($attributes as $attribute): ?>
<tr>
<td class="col label" scope="row"><?php echo $attribute->getFrontendLabel() ?></td>
<td class="col data feature" data-th="<?php echo $attribute->getFrontendLabel() ?>"><?php /* @escapeNotVerified */ echo $attribute->getFrontend()->getValue($product) ?></td>
</tr>
<?php
endforeach; ?>
</tbody>
</table>
</div>
<?php }
endforeach;
?>