How can i get last 3 added products of cart in magento.
I am using $session->getQuote()->getAllVisibleItems()
for that.
Please Provide me if any other feasible solution
You can find answer by looking at getRecentItems
method inside Mage_Checkout_Block_Cart_Sidebar
class.
/**
* Get array of last added items
*
* @param null $count
* @return array
*/
public function getRecentItems($count = null)
{
if (!$this->getSummaryCount()) {
return array();
}
if ($count === null) {
$count = $this->getItemCount();
}
return array_slice(array_reverse($this->getItems()), 0, $count);
}
This applies to current customer session.
You could just iterate through the items and stop after 3.
$product_ids = array();
$i=0;
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
$i++;
if ($i==4) break;
$product_ids[] = $item->getProduct()->getId();
}
var_dump($product_ids);
Mage_Checkout_Block_Cart_Sidebar
class.
Commented
Jan 30, 2015 at 19:41