0

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

2 Answers 2

4

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.

3
  • Can You Explain it how can i use i am new in magento.
    – Yogesh
    Commented Jan 30, 2015 at 20:52
  • @Yogesh It really depends what you want to do with these three products. Commented Jan 30, 2015 at 20:55
  • Thanks For Reply Actaully i am using this $session->getQuote()->getAllVisibleItems() and it gives me all product array so i just need last 3 products
    – Yogesh
    Commented Jan 30, 2015 at 20:57
2

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);
2
  • 1
    You should use getAllVisibleItems(). Commented Jan 30, 2015 at 19:36
  • Yes for sure. I missed that. I like your answer better about referring to the Mage_Checkout_Block_Cart_Sidebar class. Commented Jan 30, 2015 at 19:41

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.