1

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.

1 Answer 1

1

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();
5
  • Thanks for that It Works @michael
    – Prits
    Commented Jul 14, 2020 at 12:59
  • Is there a way to get only disable product using " productCollectionFactory " @MichiP
    – Prits
    Commented Jul 15, 2020 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/…
    – michi-p
    Commented Jul 15, 2020 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
    Commented Jul 16, 2020 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
    Commented Jul 16, 2020 at 13:09

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.