1

Magento 2.3.3

This is my code:

$_productCollection = $block->getLoadedProductCollection();
$values = [66];
$conditionType = "in";
$_productCollection->addCategoriesFilter([$conditionType => $values]);

I am trying to add a category filter to the collection, but it does not work. Any ideas why?

Thanks

Solution looks like this:

$_productCollection = $block->getLoadedProductCollection();
$_productCollection->clear();
$values = "66";
$conditionType = "in";
$_productCollection->addCategoriesFilter([$conditionType => $values])->load();
1
  • 1
    +1 for answer acceptance :) Nov 10, 2020 at 7:09

1 Answer 1

1

Do not use array. Just string would be fine

like this:

$_productCollection = $block->getLoadedProductCollection()->clear();
$values = "66";
$conditionType = "in";
$_productCollection->addCategoriesFilter([$conditionType => $values]);

You can also send multiple IDs like this:

$_productCollection = $block->getLoadedProductCollection()->clear();
$values = "66,67,68";
$conditionType = "in";
$_productCollection->addCategoriesFilter([$conditionType => $values]);
3
  • Thanks, but doesnt work, still getting all products ... maybe I have to reload the collection somehow? This is on list.phtml.
    – EOB
    Nov 10, 2020 at 6:55
  • YES, that made it work, Ill update my question with the solution.
    – EOB
    Nov 10, 2020 at 7:08
  • Great, good to hear that :) Nov 10, 2020 at 7:09

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.