skip to Main Content

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 MagentoFrameworkViewElementTemplate
{
protected $_attributeFactory;

public function __construct(
         MagentoCatalogModelResourceModelEavAttribute $attributeFactory

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

public function getallattributes()
{
    $arr = [];
    $attributeInfo = $this->_attributeFactory->getCollection()->addFieldToFilter(MagentoEavModelEntityAttributeSet::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;
 }                                                                    } 

3

Answers


  1. No tested but it will do job for you

    $attributeInfo = $this->_attributeFactory->getCollection()
                     ->addFieldToFilter(MagentoEavModelEntityAttributeSet::KEY_ENTITY_TYPE_ID, 4)
                     ->addFieldToFilter('is_visible_on_front',1);
    
    Login or Signup to reply.
  2. To get All product attributes you need to use inject class

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

     public function __construct(Context $context,   
        MagentoEavModelResourceModelEntityAttributeCollection $coll
        ){
     $this->_coll=$coll;
            parent::__construct($context);
    }
    
     public function getAllAttributes()
        {
            $this->_coll->addFieldToFilter(MagentoEavModelEntityAttributeSet::KEY_ENTITY_TYPE_ID, 4);
            $attrAll = $this->_coll->load()->getItems();
     echo '<pre>'; var_dump($attrAll);'</pre>';
            exit;
    }
    
    Login or Signup to reply.
  3. You can do it using the following function:

     public function getallattributes()
        {
            $arr = [];
            $attributeInfo = $this->_attributeFactory->getCollection()- 
            >addFieldToFilter(MagentoEavModelEntityAttributeSet::
            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;
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search