Question

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

Was it helpful?

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.

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top