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

3 REPLIES

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

 

 

Re: list of product attribute in magento 2 programmatically

Hello @lavkush _gupta


You can get list of product attribute whose status is enable by using this below line :

 

 

$this->_attributeFactory->getCollection()
                 ->addFieldToFilter(\Magento\Eav\Model\Entity\Attribute\Set::KEY_ENTITY_TYPE_ID, 4)
                 ->addFieldToFilter('is_visible_on_front',1);

 


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\Eav\Attribute $attributeFactory

){
	parent::__construct($context);
	$this->_attributeFactory = $attributeFactory;
}

public function getallattributes()
{
    $attribute_data = [];
    $attributeInfo = $this->_attributeFactory->getCollection()
                 ->addFieldToFilter(\Magento\Eav\Model\Entity\Attribute\Set::KEY_ENTITY_TYPE_ID, 4)
                 ->addFieldToFilter('is_visible_on_front',1);

   foreach($attributeInfo as $attributes) {
        $attributeId = $attributes->getAttributeId(); // You can get all fields of attribute here
        $attribute_data[$attributes->getAttributeId()] = $attributes->getFrontendLabel();
   }
   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