Long M K Nguyễn
Long M K Nguyễn

Reputation: 817

Magento's getItemsCollection is not applying filters

I'm trying to apply filter to the quote items collection, i get the collection using this

$items_collection = Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection();
$items_collection->addFieldToFilter('product_id', $_product->getId());

when i print out the select query, the query is correct

$items_collection->getSelect()

But when i iterate through the collection, it is fetching all of the items, it did not apply the filter, does anyone know why?

foreach ($items_collection as $item) {
    // Do things....
}

Upvotes: 0

Views: 872

Answers (1)

Amitkumar solanki
Amitkumar solanki

Reputation: 391

Try this. Use getModel instead of getSingleton.

$items_collection = Mage::getModel('checkout/session')->getQuote()->getItemsCollection();
$items_collection->addFieldToFilter('product_id', $_product->getId());

foreach ($items_collection as $item) {
echo $item->getProduct()->getId();
}

Upvotes: 1

Related Questions