1

I am getting the correct count using code.

$this->getCatalogRuleCollection()->count()

But when I am adding the filter to it is not working.
Following code is not working.

$this->getCatalogRuleCollection()->addFieldToFilter('entity_id',$entityId)->count()

Tried getSize() as well, but facing same issue.
Why is this so?
What is the reason for this?
What is the alternative(best method) to getting the count?

6
  • Hi, had you some time to test it?
    – sv3n
    Mar 20, 2018 at 22:56
  • @sv3n I will check and post the solution. Mar 21, 2018 at 4:56
  • @sv3n I have checked all the solutions posted here, but no one is working. I think addFieldToFilter loads the collection and that's why count or getSize are not working. But I am not sure. Kindly post it here if you got anything for it. Mar 21, 2018 at 6:07
  • Hey @AnshuMishra Did you found any solution related this question? Jan 16, 2020 at 9:23
  • @RohanHapani No, I didn't get any solution for this. If I remember correctly I have changed the code somewhere else to fulfill the requirement. Jan 16, 2020 at 9:29

3 Answers 3

0

Try this.

$this->getCatalogRuleCollection()->addFieldToFilter('entity_id',$entityId)->getSize();
2
  • Tried, but facing the same issue. Mar 7, 2018 at 11:40
  • Can you please share your whole so that i can understand more.
    – anonymous
    Mar 7, 2018 at 11:47
0
 $counts = $this->getCatalogRuleCollection()->addFieldToFilter('entity_id',$entityId);                                              
 print_r(count($counts));
0

Take a look at: \Magento\CatalogRule\Model\ResourceModel\Product\CollectionProcessor.php & there is a collection flag being set similarly however you may need to set or reset() the data so the getSize() will pull in non persistent and/or cached data.

$productCollection = $this->getCatalogRuleCollection();
$productCollection->addFieldToFilter('entity_id',$entityId);
$productCollection->setFlag('catalog_rule_loaded', false); // NOTE flag set
$productCollection->getSize();

or

$productCollection->setFlag('catalog_rules_dirty', true);

/**
 * Flag indicates that some rules have changed but changes have not been applied yet.
 */
class Flag extends \Magento\Framework\Flag
{
    /**
     * Flag code
     *
     * @var string
     */
    protected $_flagCode = 'catalog_rules_dirty';

Since it seems a lot of Magento 1 code still exists, may help shed more light: Reload collection after count()

1
  • How will I know which flag I have to use, I have tried with both the mentioned flag but it is working. Mar 21, 2018 at 6:08

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.