1

I need to get product collection by filter multiple category ids.

Here is my code:

$catId = [2, 3, 5];
$productcollection = $objectmanager->create('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

$categoryProducts = $productcollection->addAttributeToSelect('*')
    ->addAttributeToSort('position', 'ASC')
    ->addCategoriesFilter(['in' => $catId])
    ->addAttributeToFilter('color', ['in', $colorValues]);

But this code is not working.

1
  • Do you need sort the product by position in category?
    – aravind
    Commented May 24, 2019 at 7:11

3 Answers 3

1

Actually you pass the category ids array in addCategoriesFilter(['in' => $cat_ids]) that's why the error occur. Just Replace my code with your's

$cat_ids = "2,3,5";
$productcollection = $objectmanager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$categoryProducts = $productcollection->addAttributeToSelect('*')->addAttributeToSort('position', 'ASC')->addCategoriesFilter(['in' => $cat_ids])->addAttributeToFilter('color', array('in',$colorvalues));

I hope this will help

10
  • 1
    Thanks for reply. But this code is not working.
    – Shomita
    Commented May 23, 2019 at 4:54
  • What was the error? Have you copy paste my code. It is working at my end. Commented May 23, 2019 at 4:58
  • 1
    Getting this error------> Fatal error: Uncaught Error: Call to undefined method Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::addAttributeToSelect()
    – Shomita
    Commented May 23, 2019 at 5:08
  • Oh I got it, you have a problem in $productcollection object that's why it is unable to call addAttributeToSelect('*'). Check now I have updated my code. Commented May 23, 2019 at 5:15
  • 1
    You have two options. If you want to use parent categories Ids so You have to fetch the child categories Ids from parent Ids before passing it to filters or You have to assign your products to parent categories so that it will fetch all the product related to that specific categories Ids. If my answer is helpful please mark my answer correct and upvote my answer so it helps other as well. Commented May 23, 2019 at 6:14
3

You can add the other attribute filter. I just check multiple category filter. It is working, I just Created object again after loading model collection.

$productcollection = $obj->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory')->create();

$cat_ids = [179,109];

$categoryProducts = $productcollection->addAttributeToSelect('*');
$categoryProducts->addCategoriesFilter(array('in' => $cat_ids));

echo '<pre>';
echo count($categoryProducts);
//var_dump($categoryProducts);
foreach ($categoryProducts as $product) {
print_r($product->getData());
}
echo '</pre>';
2
protected $_productCollectionFactory;

public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
) {
    parent::__construct($context);
    $this->_productCollectionFactory = $productCollectionFactory;
}
public function getProductCollectionByCategories()
{
    $categories = [2,3,5];//category ids array
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->addCategoriesFilter(['in' => $categories]);
    return $collection;
}

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.