Magento 2: Filter Product Collection by Multiple Categories (Magento 2.1)
You are probably used to the "every method returns $this
" paradigm from Magento 1. This is not the case anymore (at least not always).
Specifically, addCategoriesFilter()
does not return anything and that's why you get null
.
Change the code to:
$productCollection = $this->productCollectionFactory->create();
$productCollection->addAttributeToSelect('*');
$productCollection->addCategoriesFilter(array('in' => $catalog_ids));
Your first try is definitely the right way of doing it:
$values = [318, 619, 620];
$conditionType = "in";
$productCollection->addCategoriesFilter([$conditionType => $values]);
Now there's two things to ensure: $productCollection
must be an instance of Magento\Catalog\Model\ResourceModel\Product\Collection
for this to work (or from a class that extends it).
And obviously you need to have products in the catalog_category_product
table that matches that condition, maybe that's not the case and that's why you get NULL.