0

Via backend, I added a dropdown custom attribute on my products. Now I need to filter the product collection based on the value of this dropdown. If I add the option id in the filter, everything works, but if I use the option value, then the filter doesn't work:


$attributeCode = "color";
$optionId      = 5;     // $product->getColor();
$optionValue   = "Red"; // $product->getAttributeText();

$product = $this->productCollectionFactory->create()
    ->addAttributeToFilter($attributeCode, $optionId)    // this works
;

$product = $this->productCollectionFactory->create()
    ->addAttributeToFilter($attributeCode, $optionValue) // this doesn't work
;

Problem: I have the values, not the ids.. is there a way I can filter the product collection using the option values instead of the option id?

2 Answers 2

0

Yes, you can filter the product collection using the option value instead of the option id in Magento 2.4. To achieve this, you need to use the addAttributeToFilter method with an array as the value parameter. Here is an example of how to do it:

$attributeCode = "color";
$optionValue   = "Red"; // $product->getAttributeText($attributeCode);

$product = $this->productCollectionFactory->create()
      ->addAttributeToFilter(
         array(
            array('attribute' => $attributeCode, 'eq' => $optionValue),
         )
      );

In the above code, we have used the eq (equal) operator to filter the product collection based on the option value. You can also use other operators like neq (not equal), in, nin, etc. depending on your requirements.

I hope this helps!

3
  • THank you but unfortunately it doesnt work, i still get collection empty
    – Lou Nik
    Mar 2, 2023 at 14:01
  • Sure, I can help you with your problem. However, in order for me to provide an accurate solution, I would need to see the code that you're working on. If you're comfortable sharing your code, please provide it so I can take a look. Otherwise, if you're not able to share your code, you could create a new instance of the problem or issue and provide me with the details so I can assist you better. Mar 2, 2023 at 14:33
  • thank you @Navneet but the relevant code is already there in my main post
    – Lou Nik
    Mar 7, 2023 at 12:18
0

Get Option id from $product Object

$attr = $product->getResource()->getAttribute('color');
if ($attr->usesSource()) {
      $optionId = $attr->getSource()->getOptionId("Red");
}

$product = $this->productCollectionFactory->create()
    ->addAttributeToFilter($attributeCode, $optionId);
3
  • Thank you but I don't have an $product object until I get it out from the collection
    – Lou Nik
    Mar 2, 2023 at 14:24
  • Okay you have only $attributeCode and $attributeValue Correct? Mar 3, 2023 at 4:49
  • yes, I need to get the product object using my custom attribute which is a dropdown type of custom attribute. My problem is that I have only the attribute code and the attribute value but not the option id so I need a way to filter the product collection via the option value and not the option id (if it exists)
    – Lou Nik
    Mar 7, 2023 at 12:20

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.