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.

  • Do you need sort the product by position in category? – aravind May 24 '19 at 7:11
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

  • 1
    Thanks for reply. But this code is not working. – Shomita May 23 '19 at 4:54
  • What was the error? Have you copy paste my code. It is working at my end. – Muhammad Hasham May 23 '19 at 4:58
  • 1
    Getting this error------> Fatal error: Uncaught Error: Call to undefined method Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::addAttributeToSelect() – Shomita May 23 '19 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. – Muhammad Hasham May 23 '19 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. – Muhammad Hasham May 23 '19 at 6:14
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;
}
2

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>';

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.