2

It is possible to do some collection in checkout/cart ? I tried this one but it doesn't work . My objectif is to get all items cart then filter them by attribute.

$_cart = Mage::getModel('checkout/cart')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('attribute', 'value')
    ->setPageSize(100);

foreach ($_cart as $item) {
    echo $item->getPrice();
}

or

$items= Mage::getSingleton('checkout/cart')->getQuote()->getItemsCollection();
// I dont know if it is possible to add a filter on it.
sv3n
  • 11,657
  • 7
  • 40
  • 73
PЯINCƎ
  • 11,669
  • 3
  • 25
  • 80

1 Answers1

2

Using addAttributeToFilter() does not work, because this collection is already loaded here.

To filter this collection can use addAttributeToFilter() and getAllIds() to just get the items you need.

# get items in cart
$items = Mage::getSingleton('checkout/cart')->getQuote()->getItemsCollection();
$items->addFieldToFilter('sku', 75782007);

$itemIds = $items->getAllIds();
foreach ($items as $item) {
    if (in_array($item->getId(), $itemIds)) {
        var_dump($item->getSku());
    }
}
sv3n
  • 11,657
  • 7
  • 40
  • 73
  • No, blank page: $items= Mage::getSingleton('checkout/cart')->getQuote()->getItemsCollection(); var_dump($items); – PЯINCƎ Jul 02 '17 at 09:59
  • @Prince please check updated answer. – sv3n Jul 21 '17 at 23:48
  • I dont know too why the foreach gives the three ! But the workaroud i m not sur that it works and is the good one.but i Will tested it tomorrow. Logically you have three items id1, id2, id3. The $itemIds output the 3 ids and you compare them in the foreach in_array(...). In the first loop $item->getId() give you id1, it exist in $itemIds ok the 2nd loop id2 ok then the last 3 ok so logically you'll have the three items. – PЯINCƎ Jul 22 '17 at 00:10
  • @Prince It's tested ... result is as expected, just can't explain "why" at the moment. Even getAllIds() is correct - otherwise it would not work. – sv3n Jul 22 '17 at 00:13
  • Ok i'll feedback you. – PЯINCƎ Jul 22 '17 at 00:15
  • @Prince Where do you want to get the filtered collection? In an observer? – sv3n Jul 22 '17 at 15:15
  • No in a chekout/cart not in an observer – PЯINCƎ Jul 22 '17 at 16:30
  • I just did the tests, indeed the foreach doesn't work, the workaound work, you know why just var_dump($items) returns nothing or your $items->debug() ? i will post a new question about the foreach – PЯINCƎ Jul 22 '17 at 19:47
  • @Prince i'm pretty sure this workaround is correct ... explanation here: https://magento.stackexchange.com/a/186001/46249 – sv3n Jul 22 '17 at 21:19
  • Yes @sv3n it really works but it doesn't mean that it's the best way :) for troubleshooting yes it will work. what questions me it's the foreach that doesn't work. – PЯINCƎ Jul 22 '17 at 21:34
  • @Prince foreach works as it should. It iterates over all 3 items, because collections IS already loaded. I think there is no better solution. getAllIds just runs one additional fetchCol query on entity_id column that has a small footprint, so dont worry about performance. – sv3n Jul 24 '17 at 22:37
  • Are you sure @sv3n ? what about if i use a new collection Mage::getModel('checkout/cart')->getCollection() ? – PЯINCƎ Jul 24 '17 at 22:39
  • @Prince Yes, I'm pretty sure about this. Creating a new collection takes more time/memory. You can try it out using Xhprof (https://magento.stackexchange.com/a/11212/46249). Hope this solves your question about group products in cart too :P – sv3n Jul 24 '17 at 22:51
  • Is it essential to install the Xhprof on server ? i tried just the firest and 2nd part but it doesn't work @sv3n – PЯINCƎ Jul 25 '17 at 00:15
  • @Prince Essential? I use it for performance optimizations, but a long time ago that I installed that. Maybe this is more helpful ... https://sonnguyen.ws/install-xhprof-in-ubuntu-14-04/. If this doesn't work too, leave I comment ... will check this during next days. – sv3n Jul 25 '17 at 00:23
  • i prefer the first link, yes i know that it's just for performance. i wanted to test it but i have some pretty error lib/Xhprof/Profiler.php on line 9 //line 9 = xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); but i just did create this class in lib/Xhprof/Profiler.php and i used it directly in some page like this: ` / some code ` I have nothing installed in server side – PЯINCƎ Jul 25 '17 at 00:32
  • Can you please leave some feedback? :) Still looking for a better solution? :) – sv3n Jul 31 '17 at 20:50
  • No, I'm not looking for that at the moment, but i don't like much the workarounds – PЯINCƎ Jul 31 '17 at 20:55
  • This is not a "workaround" per se. Since the collection is already loaded - adding one query to get results from same unloaded query is the fastest thing - I guess :P – sv3n Jul 31 '17 at 21:00
  • 1
    and +1 for your time – PЯINCƎ Aug 01 '17 at 09:09
  • 1
    Soon the 5000 points ^^ – PЯINCƎ Aug 01 '17 at 09:10