I want to retrieve all product attributes those are available, then convert it to name & value for my select options field. In Magento 1 I can achieve it like this:
public function getMagentoAttributes()
{
$values[] = array(
'value' => '',
'label' => 'Pick Product Attribute'
);
$categories = Mage::getResourceModel('catalog/product_attribute_collection')->getItems();
foreach ($categories as $category) {
if ($category->getFrontendLabel() != '') {
$label = $category->getFrontendLabel();
} else {
$label = $category->getAttributecode();
}
$values[] = array(
'value' => $category->getAttributecode(),
'label' => $label
);
}
return $values;
}
Is there a way in magento 2 to do the same thing?
-
I have used code according to "RonakChauhan" it is working fine in my block file But I am facing some issue i need help in that I am unable to filter attributes according to their visibility i.e I need the attributes whose status is set "visible => yes" in Admin...Any Help will be Appreciated... Here is my Code for getting Product Attribute's collection class ProductList extends \Magento\Framework\View\Element\Template{ protected $_attributeFactory; public function __construct( \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory ){ parent::__construct($context); $this->_attribGurjeet Singh– Gurjeet Singh2018年02月27日 09:58:00 +00:00Commented Feb 27, 2018 at 9:58
2 Answers 2
protected $_attributeFactory;
public function __construct(
....
\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory,
....
) {
....
$this->_attributeFactory = $attributeFactory;
....
}
public function <func_name>()
{
$attributeInfo = $this->_attributeFactory->getCollection();
foreach($attributeInfo as $attributes)
{
$attributeId = $attributes->getAttributeId();
// You can get all fields of attribute here
}
}
Here you can have whole collection of attributes, you can filter it as per your need.
-
how to get the attribute name and id?simple guy– simple guy2017年03月10日 07:17:15 +00:00Commented Mar 10, 2017 at 7:17
-
using
foreachyou can getgetAttributeId()as well asgetAttributeName()Ronak Chauhan– Ronak Chauhan2017年03月10日 07:19:37 +00:00Commented Mar 10, 2017 at 7:19 -
Check updated answerRonak Chauhan– Ronak Chauhan2017年03月10日 07:21:44 +00:00Commented Mar 10, 2017 at 7:21
-
getAttributeName print blanksimple guy– simple guy2017年03月10日 07:24:07 +00:00Commented Mar 10, 2017 at 7:24
-
1
echo "<pre>"; print_r($attributes);exit;use this in foreach and checkRonak Chauhan– Ronak Chauhan2017年03月10日 07:25:29 +00:00Commented Mar 10, 2017 at 7:25
Another idea is that we should try with Service Contracts Layer.
Use Magento\Eav\Api\AttributeRepositoryInterface to get the eav attribute.
I have an answer already here: https://magento.stackexchange.com/a/161426/33057
For example:
$searchCriteria = $this->searchCriteriaBuilder->create();
$attributeRepository = $this->attributeRepository->getList(
'catalog_product',
$searchCriteria
);
foreach ($attributeRepository->getItems() as $items) {
$items->getAttributeCode();
$items->getFrontendLabel();
}
NOTE: For the entity type code in getList method, we can find in the eav_entity_type table.
-
What is 'catalog_product' as an argument in getList()??VishalParkash– VishalParkash2020年10月23日 10:25:14 +00:00Commented Oct 23, 2020 at 10:25
-
This should be the accepted answer!Maikel Koek– Maikel Koek2021年06月04日 15:01:44 +00:00Commented Jun 4, 2021 at 15:01