how to get the list of product attribute in magento 2 programmatically
Hello @lavkush _gupta
eav_entity_type contain entity name you can find 4 entity_id for catalog_product
so you can get all attribute related to the product using below query
select * from eav_attribute where entity_type_id=4
by using above query you will get all data
more info you can find
https://www.mageplaza.com/how-use-code-external-file-script-magento-2.html
https://webkul.com/blog/magento2-write-custom-mysql-query/
final answer of your question
<?php use Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; $params = $_SERVER; $bootstrap = Bootstrap::create(BP, $params); $obj = $bootstrap->getObjectManager(); $state = $obj->get('Magento\Framework\App\State'); $state->setAreaCode('adminhtml'); $resource = $obj->get('Magento\Framework\App\ResourceConnection'); $connection = $resource->getConnection(); $tableName = $resource->getTableName('eav_attribute'); $sql = "Select * FROM " . $tableName " where entity_type_id=4"; $result = $connection->fetchAll($sql); echo "<pre>"; print_R($result); exit;
Hope it will help you.
If works then mark as solution
Hello @lavkush _gupta
You can use this below code in your block and get data using getallattributes() function call in phtml file.
protected $_attributeFactory; public function __construct( \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeFactory ){ parent::__construct($context); $this->_attributeFactory = $attributeFactory; } public function getallattributes() { $attribute_data = []; $attributeInfo = $this->_collectionFactory->create() foreach ($attributeInfo as $items) { $attribute_data[] = $items->getData(); } } return $attribute_data; }
I Hope it will help you.
If issue solved , Click Kudos & Accept as Solution
Maybe that will be the solution for you: check the https://github.com/swissup/featured-attributes free module from SwissupLabs that shows attributes at listing for each product.
Hello @rohanhapani
method getCollection is deprecated
@deprecated 101.0.0 because collections should be used directly via factory
Hello @andrewbess
I changed my answer. You can check it. If it'll helpful for you. You can give kudos
$product = $this->_productRepository->get("PRODUCTSKU");
$attributes = $product->getAttributes();
foreach($attributes as $a)
{
echo $a->getName()."\n";
}
by this you get the list of product attribute in magento 2 programmatically