0

I'm trying to get product collection by custom attribute "Age"
Age is a Dropdown attribute

Please help me to get this solution?

2 Answers 2

1

You can filter the collection by attribute code and Option ID.

You can use addAttributeToFilter for collection filter by using a custom attribute.

protected $productCollectionFactory;

public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory  $productCollectionFactory

){
     $this->productCollectionFactory = $productCollectionFactory;
}

$productcollection = $this->productCollectionFactory
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('attribute code', 'option id');
0

For the drop-down attribute, We can filter using drop-down option ID instead of Label.

Your code is like below.

$productcollection = $this->_productCollection
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('age', 18)
    ->load();
1
  • Please update code to load option Id by option label, then it will be useful
    – Manjunath
    Apr 8, 2020 at 14:50

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.