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.
The call to
Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
... is actually clearing the session variable after it is read. Magento uses magic methods extensively. In this case you are using the __call()
magic method which in turn uses the getData()
method. In Mage_Core_Model_Session_Abstract_Varien
you will see that they override the default behaviour of getData()
to expect the second parameter to be a boolean (The first parameter to getData()
is the key name for the value you are looking for). That boolean is a flag telling the session to clear the variable after reading.
You could always listen for the checkout_cart_product_add_after
event and add the item to your own variable in the session. That event is actually fired on the line before setLastAddedProductId()
is called.
Refered links:
Edit 1:
You're almost there - try this code:
$collection = Mage::getSingelton('checkout/session')->getQuote()->getItemsCollection();
$collection->getSelect()->order('created_at DESC');
$latestItem = $collection->getFirstItem();
Note that when you get the latest quote item, you're not actually obtaining the product. To get the actual product, you would need to add this line:
$product = $latestItem->getProduct();
Edit 2:
Use this below code
Code to get the the full product information (where 3 is the configurable product Id)
$product = Mage::getModel('catalog/product')->load(3);
$childProducts = Mage::getModel('catalog/product_type_configurable')
->getUsedProducts(null,$product);
foreach($childProducts as $child) {
print_r($child->getName()); // You can use any of the magic get functions on this object to get the value
}
Edit 3:
You can also get by sku
$associated_products = $_product->loadByAttribute('sku', $_product->getSku())->getTypeInstance()->getUsedProducts();
foreach ($associated_products as $assoc)
{
$assocProduct = Mage::getModel('catalog/product')->load($assoc->getId());
}
Best Answer