0

How can i count an productcollection without changing it?

when i do:

$test = Mage::getSingleton('catalog/layer')->getProductCollection()->addFieldToFilter('special_price', 150);

And then call count($test) it replaces my productcollection.

EDIT:

Basicly i want this:

Mage::getSingleton('catalog/layer')->getProductCollection()->addFieldToFilter('special_price', array('gt' => 0))->getSize();

To count how many products has a special price. But when i call this code, it replaces my product collection, so it only shows products with special_price

2
  • did you try to use $test->getSize() ? Oct 28 '16 at 12:07
  • The problem is, it replaces my productcollection even before i call $test
    – RappY
    Oct 28 '16 at 12:12
0

Please use $count = $test->getSize();

dont use count directly to collection.

3
  • Ok. I will. But... still doesnt solve my problem with the productcollection getting replaced :-(
    – RappY
    Oct 28 '16 at 12:13
  • Brittany, would you like to post the code for collection? So that we can easily understand and solving your problem quickly. Oct 28 '16 at 12:27
  • I tried to edit my question now. Hope it makes sense :-)
    – RappY
    Oct 28 '16 at 12:30
0

Try this :

Mage::getSingleton('catalog/layer')->getProductCollection()->addFieldToFilter('special_price', 150)->count();

OR

Mage::getModel('catalog/layer')->getProductCollection()->addFieldToFilter('special_price', 150)->count();
5
  • Same result, still replacing my active collection :/
    – RappY
    Oct 28 '16 at 12:46
  • @BrittanyMoore Pl check my updated answer Oct 28 '16 at 12:47
  • The counting is perfect.. but the problem is, it replaces all my products, so it only shows those with special price at 150... i only want the number, not to replace all products :/
    – RappY
    Oct 28 '16 at 12:49
  • @BrittanyMoore, now check updated answer Oct 28 '16 at 13:06
  • But the problem with getmodel is, that i dont get the sorting it is already making, so basicly i want the active collection to be counted, not a new one
    – RappY
    Oct 28 '16 at 13:08
0

So i found a method.

Mage::getSingleton('catalog/layer')
->getProductCollection()
->getSelectCountSql()
->where('special_price > 0')
->query()
->fetchColumn();

This seems a bit odd... so please tell me, if anyone got a better solution.

0

Just make a clone of that collection then you will get count from clone collection

$cloneLayerCollection =clone Mage::getSingleton('catalog/layer')->getProductCollection();
$size = $cloneLayerCollection->addFieldToFilter('special_price', array('gt' => 0))->getSize();
1
  • I tried that, but actually it still makes changes to my collection on the cloned one.
    – RappY
    Oct 28 '16 at 13:49
0

EAV queries work by creating conditions and inner joins against related tables. So even if you were to remove the where clause of your select statement, you would still be left with an inner join that would mess up your query.

You could perform a reset(), but you would be left with an empty query.

The initial select statement is built with the _initSelect() method, which is protected, and is called in the constructor of the parent class.

Your best bet is to instantiate a new instance to establish the count.

$countCollection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToFilter('special_price', '150)
    ->getSize();

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.