1

I'm using Magento 2, when I try add product in Backend, in the Configurations tab, I have created Configuration and I saw there three attributes.

enter image description here

How can I get them in a module on frontend?

I see they are stored in eav_attribute table but I dont know which SQL can be do it, because it has no conditional column

enter image description here

Thank so much!

0

2 Answers 2

1

based on this answer , you can get all the attributes and then filter it using the entity_type_id

public function __construct(
    \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
    \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository,
    ......
)
{
    $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    $this->attributeRepository = $attributeRepository;
    ......
}


public function execute(){
    $searchCriteria = $this->searchCriteriaBuilder->create();
    $attributeRepository = $this->attributeRepository->getList(
        'catalog_product',
        $searchCriteria
    );

    foreach ($attributeRepository->getItems() as $items) {
        $items->getAttributeCode();
        $items->getFrontendLabel();
    }
}
3
  • Hi, thank you, I have tried do mysql query with entity_type_id = 4 (catalog_product), it's have 63 results including 3 attributes, not only 3 attributes like my first screenshot. Mar 15, 2017 at 1:38
  • @HữuCôngHoàng see my updated answer Mar 15, 2017 at 2:39
  • Hi, I have puted the code in my controller, but it showed 63 attributes too, same mysql query. Mar 15, 2017 at 7:26
0

You can use this,

namespace NameSpace\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{    
protected $_productRepository;

public function __construct(
    \Magento\Backend\Block\Template\Context $context,        
    \Magento\Catalog\Model\ProductRepository $productRepository,
    array $data = []
)
{
    $this->_productRepository = $productRepository;
    parent::__construct($context, $data);
}

public function getProductById($id)
{
    return $this->_productRepository->getById($id);
}

public function getProductBySku($sku)
{
    return $this->_productRepository->get($sku);
}
}

and in your phtml.

$id = YOUR_PRODUCT_ID;
$product = $block->getProductById($id); // load product bi ID
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) { 
   echo $attribute->getAttributeCode(); echo '<br />';
   echo $attribute->getStoreLabel(); echo '<br />';
   echo $attribute->getFrontendLabel(); echo '<br />';    
   echo $attribute->getFrontend()->getLabel(); echo '<br />';

// you might not get value for all attributes
   echo $attribute->getFrontend()->getValue($product); echo '<br />';    
}
2
  • 1
    Hi, Thank you, but as description I want get the list of attributes, not list of attributes of a product. Mar 14, 2017 at 12:31
  • @HữuCôngHoàng: did you get the answer? Oct 23, 2020 at 10:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.