23

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.

5
  • 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

29

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

Example:

// 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:

Customers

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

Orders

$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`);
5

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:

$collection->getSelect()->limit(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);
var_dump($collection->getSelect()->__toString());
var_dump($collection->getFirstItem()->getData());

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
1
  • 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
4
var_dump($_collection->getFirstItem()->getData());
echo $_collection->getFirstItem()->getName();

Also how to get Last Item:

echo $_collection->getLastItem()->getName();
var_dump($_collection->getLastItem()->getData()); 
2

@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()
0
$Collection = Mage::getResourceModel('catalog/products_collection');
$firstItem = $Collection->getFirstItem(); 
0

Try this please

    $_category  = $this->getCurrentCategory();        
    $_collection = $_category->getProductCollection();        
    $_items = $_productCollection->getItems(); 
   if($_items){  
    $_items[0]->getShortName();
    $_items[0]->getProductUrl();
   }

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.