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.
0

1 Answer 1

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());
    }
}
22
  • No, blank page: $items= Mage::getSingleton('checkout/cart')->getQuote()->getItemsCollection(); var_dump($items);
    – PЯINCƎ
    Jul 2, 2017 at 9:59
  • @Prince please check updated answer.
    – sv3n
    Jul 21, 2017 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, 2017 at 0:10
  • 1
    and +1 for your time
    – PЯINCƎ
    Aug 1, 2017 at 9:09
  • 1
    Soon the 5000 points ^^
    – PЯINCƎ
    Aug 1, 2017 at 9:10

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.