On the category view.phtml if I get the category and then get a product collection, how can I get the first product form the collection assigned to $_product for use in the code? Here is what I have:

$_category   = $this->getCurrentCategory();
$_collection = $_category->getProductCollection();
$_product    = 
$_resource   = $_product->getResource();

I tried using foreach but kept getting errors.

  • 2
    $_product = $_collection->getFirstItem()
    – pzirkind
    Commented Jun 16, 2014 at 18:33
  • 1
    @pzirkind put this answer.please don't put it comment
    – Amit Bera
    Commented Jun 17, 2014 at 7:49
  • 1
    I tried this but getting error - Invalid method Mage_Catalog_Block_Category_View::canEmailToFriend(Array))
    – danny
    Commented Jun 17, 2014 at 8:48
  • 1
    @heisenberg that meand you are trying to call a function that doesn't exixt, either way, its a different error (not connected with your original question), either update your question with the new code or start a new one
    – pzirkind
    Commented Jun 17, 2014 at 13:11
  • @danny if you found an answer here, please accept to mark this question as resolved.
    – sv3n
    Commented Jul 6, 2017 at 0:24

6 Answers 6


To get the first item in a collection simply use the getFirstItem() function on the collection.


// this gets all the products
$productCollection = Mage::getResourceModel('catalog/products_collection');
// this line gets just the first product
$firstItem = $productCollection->getFirstItem(); 

Example 2 (for this particular question):

$_category  = $this->getCurrentCategory();
$_collection = $_category->getProductCollection();
$_product =  $_collection->getFirstItem(); // this will get first item in collection

Some other areas this can be used:


$customerCollection = Mage::getResourceModel('customer/customer_collection');
$firstCustomer = $customerCollection->getFirstItem();


$orderCollection = Mage::getResourceModel('sales/order_collection');
$firstOrder = $orderCollection->getFirstItem();

Please Note:

Its not a good idea to load ALL the products/customers/orders for this takes a lot of resources. The preferred way is to limit what you want to load by using the addAttributeToFilter() or addFieldToFilter() functions, see example below:

$productCollection = Mage::getResourceModel('catalog/product_collection')
                           ->addAttributeToFilter('sku', 'book123`);

pzirkind is totally correct, just wondering that nobody takes care about performance. If you ONLY want the first/last item of a collection, you should always limit your query to 1:


Just using getFirstItem() will still loads the whole collection and after that it grags the first item.

Example: category with 750 products

$category = Mage::getModel('catalog/category')->load(41);
$collection = $category->getProductCollection();
# $collection->getSelect()->limit(1);

Just getFirstItem():

  • Total Incl. Wall Time (microsec): 2,318,497 microsecs
  • Total Incl. CPU (microsecs): 2,000,604 microsecs
  • Total Incl. MemUse (bytes): 7,729,776 bytes
  • Total Incl. PeakMemUse (bytes): 7,977,672 bytes
  • Number of Function Calls: 96,957

Adding getSelect()->limit(1):

  • Total Incl. Wall Time (microsec): 424,955 microsecs
  • Total Incl. CPU (microsecs): 380,326 microsecs
  • Total Incl. MemUse (bytes): 4,043,728 bytes
  • Total Incl. PeakMemUse (bytes): 3,976,000 bytes
  • Number of Function Calls: 15,249
  • 2
    This is ideal as it covers two solutions, and supplies execution times, and explains performance benefits +1
    – BENN1TH
    Commented Mar 30, 2018 at 22:51
echo $_collection->getFirstItem()->getName();

Also how to get Last Item:

echo $_collection->getLastItem()->getName();

@pZirKind is right you can get the first item from the products collection using the native methods of Varien Collection class like:

$_category  = $this->getCurrentCategory();

$_collection = $_category->getProductCollection();

$_product = $_collection->getFirstItem()
$Collection = Mage::getResourceModel('catalog/products_collection');
$firstItem = $Collection->getFirstItem(); 

Try this please

    $_category  = $this->getCurrentCategory();        
    $_collection = $_category->getProductCollection();        
    $_items = $_productCollection->getItems(); 

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.