1

magento 1 code to get all the attributes collection

public function getAttributes()
    {
        $collection = Mage::getResourceModel('catalog/product_attribute_collection')->addVisibleFilter()->addIsFilterableFilter();
        return $collection;
    }

I am changing magento1 code to magento 2 like below It get the collection but while applying filters like addVisibleFilter(),addIsFilterableFilter() then it is failed to load the collection. Please provide me a solution to get all the attributes with filters.

protected $_attributeFactory;

 public function __construct(
    ....
    \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory,
    ....
) {
    ....
    $this->_attributeFactory = $attributeFactory;
    ....
}

public function <func_name>()
{
    $attributeInfo = $this->_attributeFactory->getCollection()->addVisibleFilter()->addIsFilterableFilter();

 }
1

Suggesting you use

\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeFactory

instead of

\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory

Then use

$collection = $this->_attributeFactory->create()->addVisibleFilter();
0

Try this,

  $collection = $this->_attributeFactory->create();
  $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
  $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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