Magento – Order email, how to get the product Id

configurable-productemail-templatesmagento-1.8

in /app/design/frontend/base/default/template/email/order/items/order/default.phtml

my new order email lists the configurable products the customer has ordered

I want to output the product id of the product that was selected, however:

$_item->getProductId()

gives me the configurable product ID and not the child (simple) product that was chosen by the customer.

in the basket, I did this:

$product = Mage::getModel('catalog/product')->load($_item->getProduct()->getId());
if($product->getTypeId() == "configurable")
{
  $product = Mage::getModel('catalog/product')->load($_item->getOptionByCode('simple_product')->getProductId());  
} 
echo $product->getId() ;

but on this page i get:

Fatal error: Call to a member function getProductId() on a non-object

Best Answer

Managed to fix this with:

$product = Mage::getModel('catalog/product')->load($_item->getProduct()->getId());
if($product->getTypeId() == "configurable")
{
  $options = $_item->getProductOptions() ;
  $sku = $options['simple_sku'] ;
  $productid = Mage::getModel('catalog/product')->getIdBySku($sku);
  $product = Mage::getModel('catalog/product')->load($productid) ;
}