I have a Custom attribute on Product in which i have save a string i want to get count of those products which have same attribute value in Magento 2 using an Controller or Model.


You have to inject the class \Magento\Catalog\Ui\DataProvider\Product\ProductCollectionFactory

$productCollection = $this->productCollectionFactory->create();
$productCollection->addFieldToSelect('*')->addFieldToFilter('your_attribute', ['eq' => 'your_value']);
$numberOfProducts = $productCollection->count();
  • Thanks for that It Works @michael – Prits Jul 14 '20 at 12:59
  • Is there a way to get only disable product using " productCollectionFactory " @MichiP – Prits Jul 15 '20 at 4:54
  • I think it's not that easy to get a collection of disabled products in a Magento frontend. (It's not like adding another addFiledToFilter). However, I found this answer here, maybe it helps you: magento.stackexchange.com/questions/276854/… – MichiP Jul 15 '20 at 15:07
  • Thanks for the link i tried that before but it seems doesn't work @MichiP will you please look into this question and help me magento.stackexchange.com/questions/317376/… – Prits Jul 16 '20 at 7:12
  • Is there a way to get next 3 product from productCollectionFactory . Ex: Like offset type means not show starting 3 product then show product after that . – Prits Jul 16 '20 at 13: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.