cancel
Showing results for 
Search instead for 
Did you mean: 

list of product attribute in magento 2 programmatically

list of product attribute in magento 2 programmatically

how to get the list of product attribute in magento 2 programmatically

6 REPLIES 6

Re: 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://magento.stackexchange.com/questions/163743/magento-2-get-all-product-attributes-without-prod...

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

 

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: list of product attribute in magento 2 programmatically

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

Re: list of product attribute in magento 2 programmatically

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.

 

Featured Attributes

Did you find it helpful? Please give "Kudos" or "Accept as Solution".
Meet 70+ extensions and templates for M1 & M2 in one place


Re: list of product attribute in magento 2 programmatically

Hello @rohanhapani 
method getCollection is deprecated 

@deprecated 101.0.0 because collections should be used directly via factory

 

We need to write great code

Re: list of product attribute in magento 2 programmatically

Hello @andrewbess 

I changed my answer. You can check it. If it'll helpful for you. You can give kudos Smiley Happy

Re: list of product attribute in magento 2 programmatically

$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