0

I want to remove last item from product collection

$collection = Mage::getModel('catalog/product')->getCollection();

But I have to avoid any 'loop' solutions.

3

Without context, I cannot say if this is a good idea, but if you have a loaded collection and want to remove the last item from the collection (not delete it), you can do this:

$lastId = $collection->getLastItem()->getId();
$collection->removeItemByKey($lastId);

This works because internally the items are stored in an array with their id as key.

2

Additional database query but retaining collection:

$collection     = Mage::getModel('catalog/product')->getCollection();
$collectionSize = $collection->getSize();
$collection->getSelect()->limit(--$collectionSize);

Basic PHP but out of collection scope:

$collection = Mage::getModel('catalog/product')->getCollection();
$products   = $collection->getItems();
array_pop($products);

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.