I have created a custom product attribute that needs to be inserted into sales_flat_order_item (there's a custom column already created) everytime an order is placed. Config.xml for the observer event:
<events>
<sales_order_place_after>
<observers>
<test_modul_model_observer>
<type>singleton</type>
<class>Test_Modul_Model_Observer</class>
<method>orderPlaceAfter</method>
</test_modul_model_observer>
</observers>
</sales_order_place_after>
</events>
Observer.php:
<?php
class Test_Modul_Model_Observer
{
public function orderPlaceAfter($observer)
{
$order = $observer->getEvent()->getOrder();
foreach ($order->getAllItems() as $item) {
//$item is an instance of Mage_Sales_Model_Order_Item
$product = Mage::getModel('catalog/product')->load($item->getProductId());
}
$articleNumber = $product->getArticleNumber();
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$lastInsertId = $connection->lastInsertId();
$sql = "UPDATE sales_flat_order_item SET article_number = '" . $articleNumber . "' where item_id = ". $lastInsertId;
$connection->query($sql);
Mage::log($sql, null, "order.log", true);
}
}
The problems is... the $lastInsertId
is 0
so it doesn't know what column to update. It supposed to be the last record inserted (after I complete the order).
Any help would be appreciated. Thanks!