0

Issue is I am getting all the attribute collection not the attributes which are visible on frontend I am unable to filter them as the data which is coming is from Table: eav_attribute from there we can't filter it as in that table there is no column for "is_visible_on_frontend" Actually data I need is existing in Table: catalog_eav_attribute there is column for visibility. Please check my code which I am using in my custom module block file.

Main Function for calling attribute is getallattributes() & Model is \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory

class ProductList extends \Magento\Framework\View\Element\Template
{

protected $scopeConfig;
protected $_attributeFactory;
protected $displayattributes;

/**
 * @return string
 */
public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
         \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
         \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory,
          \Ghrix\PrintCatalog\Model\Config\Source\Displayattributes $displayattributes
         ){
    parent::__construct($context);
    $this->scopeConfig = $scopeConfig;
    $this->_attributeFactory = $attributeFactory;
    $this->displayattributes = $displayattributes;
}

public function getcategoryId()
{
    if (!$this->scopeConfig->isSetFlag('modulesettings/catalogprint/settings', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
     return ;
    }
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
    $catid = $category->getId();

    return $catid; 

}
public function getcategoryname()
{
    if (!$this->scopeConfig->isSetFlag('modulesettings/catalogprint/settings', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
     return ;
    }
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
    $catname = $category->getName();
    return $catname; 

}
public function productswithprices()
{
    if (!$this->scopeConfig->isSetFlag('modulesettings/catalogprint/settings', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
     return ;
    }
    $myvalue = $this->scopeConfig->getValue('modulesettings/catalogprint/productswithprices', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    return $myvalue;
}
public function productswithrrp()
{
    if (!$this->scopeConfig->isSetFlag('modulesettings/catalogprint/settings', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
     return ;
    }
    $myvalue = $this->scopeConfig->getValue('modulesettings/catalogprint/productswithrrp', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    return $myvalue;
}
public function productswithsku()
{
    if (!$this->scopeConfig->isSetFlag('modulesettings/catalogprint/settings', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
     return ;
    }
    $myvalue = $this->scopeConfig->getValue('modulesettings/catalogprint/productswithsku', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    return $myvalue;
}

public function displaymode()
{
    if (!$this->scopeConfig->isSetFlag('modulesettings/catalogprint/settings', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
     return ;
    }
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
    $mode  = $category->getDisplayMode();
    return $mode;
}
public function selectedattributes()
{
    if (!$this->scopeConfig->isSetFlag('modulesettings/catalogprint/settings', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
     return ;
    }
    $entries = $this->scopeConfig->getValue('modulesettings/catalogprint/displayattributes', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    $entries = explode(',', $entries);
    $displayattributes = $this->displayattributes->getOriginalOption();
    $result = array();
    foreach($displayattributes as $key => $value) 
    {
        if(in_array($key, $entries)) 
        {
        $result[] = $value;
        }
    }
        return $result;
}

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;

}


}

1 Answer 1

0

You can do,

    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
             if($attributes->getIsVisibleOnFront()){
                  $arr[$attributes->getAttributeId()] = $attributes->getFrontendLabel();
             }


        }
        return $arr;

     } 
1
  • Not working this solution. Commented Apr 20 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.