4

I have overridden the Mage_ImportExport_Model_Import_Entity_Abstract class and in the function _saveValidatedBunches() I want to get the last created product Id. I've tried the following, but it returns null.

$collection = Mage::getModel('catalog/product')->getCollection();
$latestItemId = $collection->getLastItem()->getId();
1
  • you can check magento function getLastItem at lib\Varien\Data\Collection.php
    – Ami Kamboj
    Jun 5, 2014 at 10:56

3 Answers 3

10

you can try like below with latest created_at

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

   $collection->getSelect()->limit(1);

EDIT

  echo $latestItemId = $collection->getLastItem()->getId();
  exit;

hope this will sure help you.

1
  • @liyakat: Thanks a lot :) and +1, Your answer worked perfectly. I'll accept it
    – Sukeshini
    Jun 9, 2014 at 4:07
6

Try this:

$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addAttributeToSort('entity_id', 'DESC');
$collection->setPage(1,1); //set a limit to 1 so the query would be faster
echo $collection->getFirstItem()->getId();
5
  • Marius: it will gives first not last created.
    – Ami Kamboj
    Jun 5, 2014 at 11:25
  • @AmitKamboj. Yep. You are right. I will come up with an other solution in a few moments. Looks like getAllIds resets the order.
    – Marius
    Jun 5, 2014 at 11:30
  • Change the answer to a working solution.
    – Marius
    Jun 5, 2014 at 11:34
  • Yes Marius i have checked its working :)
    – Ami Kamboj
    Jun 5, 2014 at 11:35
  • Thanks a lot Marius. As always your answer worked :) and +1
    – Sukeshini
    Jun 9, 2014 at 4:10
1

This will sort products by create date desc:

$todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

$collection = Mage::getModel('catalog/product')
                    ->getCollection()                   
                    ->addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))
                    ->addAttributeToFilter('news_to_date', array('or'=> array(
                        0 => array('date' => true, 'from' => $todayDate),
                        1 => array('is' => new Zend_Db_Expr('null')))
                    ), 'left')
                    ->addAttributeToSort('news_from_date', 'desc')
                    ->addAttributeToSort('created_at', 'desc');   

Then limit it to one to get the last created:

$collection->getSelect()->limit(1);
1
  • Thanks for the answer, But if I took first item in the collection or last item id, it returns null. I tried with $collection->getLastItem()->getId(); and $collection->getFirstItem()->getId();. But both are null
    – Sukeshini
    Jun 9, 2014 at 4:17

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.