Question popped up here: Filter collection on checkout/cart
Why does a filtered $collection->getSize()
returns correct value, but iterating over collection has more items?
- three items are in cart
- want to filter one by SKU
- result should be only one item
Example code:
$items = Mage::getSingleton('checkout/cart')->getQuote()->getItemsCollection();
$items->addFieldToFilter('sku', 75782007);
Using var_dump($items->getSize())
correctly returns int 1
, but ...
foreach ($items as $item) {
var_dump($item->getSku());
}
... results in ... (why?)
string '75782007' (length=8)
string '32811969' (length=8)
string 'hdd006' (length=6)
Workaround:
$itemIds = $items->getAllIds();
foreach ($items as $item) {
if (in_array($item->getId(), $itemIds)) {
var_dump($item->getSku());
}
}
... results in ...
string '75782007' (length=8)
Why is getSize()
and getAllIds()
correct, but does not fit to collection count?