php - Magento 2 : How to get product attributes collection in custom module block file whose status is visible =>yes?

349

Here is my function for calling product attribute collection I have already get product attributes for the product's which are enabled but I am having issue in filtering them according to their own visibility i.e I want only those product attributes collection whose status is set visible from admin....

class ProductList extends \Magento\Framework\View\Element\Template
{
protected $_attributeFactory;

public function __construct(
         \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory

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

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

   foreach($attributeInfo as $attributes)
   {
        $attributeId = $attributes->getAttributeId();
        // You can get all fields of attribute here

         $arr[$attributes->getAttributeId()] = $attributes->getFrontendLabel();


   }
   return $arr;
 }                                                                    } 
932

Answer

Solution:

No tested but it will do job for you

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

Answer

Solution:

To get All product attributes you need to use inject class

app/code/Mendor/Mymodule/Model/Attributes.php

 public function __construct(Context $context,   
    \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $coll
    ){
 $this->_coll=$coll;
        parent::__construct($context);
}

 public function getAllAttributes()
    {
        $this->_coll->addFieldToFilter(\Magento\Eav\Model\Entity\Attribute\Set::KEY_ENTITY_TYPE_ID, 4);
        $attrAll = $this->_coll->load()->getItems();
 echo '<pre>'; var_dump($attrAll);'</pre>';
        exit;
}
561

Answer

Solution:

You can do it using the following function:

 public function getallattributes()
    {
        $arr = [];
        $attributeInfo = $this->_attributeFactory->getCollection()- 
        >addFieldToFilter(\Magento\Eav\Model\Entity\Attribute\Set::
        KEY_ENTITY_TYPE_I D, 4);
    
        foreach($attributeInfo as $attributes)
        {
            $attributeId = $attributes->getAttributeId();
            // You can get all fields of attribute here
            if($attributes->getIsVisibleOnFront()){
                $arr[$attributes->getAttributeId()] = $attributes 
                     >getFrontendLabel();
            }
        }
        return $arr;
    }

People are also looking for solutions to the problem: php - how to create swagger json routes using laravel

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.