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
Magento 2 – Get Product Attribute by Attribute Group
magento-2.1magento2
Related Solutions
I suggest you should use Magento\Eav\Api\AttributeManagementInterface to get all attributes by attribute set id
To do this, we need to override Magento\Catalog\Block\Product\ListProduct class and inject this class into the constructor. See more detail here:
- app/code/YourVendor/YourModule/etc/frontend/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Block\Product\ListProduct" type="YourVendor\YourModule\Block\Catalog\Product\ListProduct" />
</config>
- app/code/YourVendor/YourModule/Block/Catalog/Product/ListProduct.php
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Framework\Data\Helper\PostHelper;
use Magento\Framework\Url\Helper\Data;
use Magento\Eav\Api\AttributeManagementInterface;
class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
{
private $attributeManagement;
/**
* @param Context $context
* @param PostHelper $postDataHelper
* @param Resolver $layerResolver
* @param CategoryRepositoryInterface $categoryRepository
* @param Data $urlHelper
* @param AttributeManagementInterface $attributeManagement
* @param array $data
*/
public function __construct(
Context $context,
PostHelper $postDataHelper,
Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository,
Data $urlHelper,
AttributeManagementInterface $attributeManagement,
array $data = []
) {
$this->attributeManagement = $attributeManagement;
parent::__construct(
$context,
$postDataHelper,
$layerResolver,
$categoryRepository,
$urlHelper,
$data
);
}
/**
* Get Attributes by Attribute Set ID
* @param $attributeSetId
* @return \Magento\Eav\Api\Data\AttributeInterface[]
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getAttributesBySetId($attributeSetId)
{
$attributes = $this->attributeManagement->getAttributes(\Magento\Catalog\Model\Product::ENTITY,
$attributeSetId);
return $attributes;
}
}
- app/design/frontend/YourCustomTheme/templates/product/list.phtml
........
<?php $attributes = $block->getAttributesBySetId($_product->getAttributeSetId()); ?>
<?php
foreach ($attributes as $attribute) {
echo ($attribute->getFrontendLabel()).':::';
}
?>
..........
Hopefully this will solve your issue
Regards
you can add this to your class:
protected $attributeRepository;
protected $searchCriteriaBuilder;
protected $sortOrderBuilder;
public function __construct(
....
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository,
\Magento\Framework\Api\SortOrderBuilder $sortOrderBuilder,
....
)
{
....
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->attributeRepository = $attributeRepository;
$this->sortOrderBuilder = $sortOrderBuilder;
....
}
Now you added the dependencies. You need a method to retrieve the attributes based on the attribute group id.
public function getAttributes($groupId)
{
$sortOrder = $this->sortOrderBuilder
->setField('sort_order')
->setAscendingDirection()
->create();
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(\Magento\Eav\Api\Data\AttributeGroupInterface::GROUP_ID, $groupId)
->addFilter(\Magento\Catalog\Api\Data\ProductAttributeInterface::IS_VISIBLE, 1) //if you want only visible attributes
->addSortOrder($sortOrder)
->create();
$groupAttributes = $this->attributeRepository->getList($searchCriteria)->getItems();
return $groupAttributes;
}
Best Answer
you simply get all product attribute by
$product->getAttributes();