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