I've got an observer that is registered and firing on the sales_order_place_after event. However when I try and call getAllVisibleItems() on the order the entire process errors out.
I've tested the code outside of placing an order, so I didn't have to complete the checkout process each time, by using
$order->Mage::getModel('sales/order')->loadByIncrementId($ordernumber);
My event Observer is as follows:
class Shopatron_OrderExport_Model_Observer
{
public function exportOrder(Varien_Event_Observer $observer)
{
Mage::log('Shopaton Order Observer', null, 'Shopatron.log');
$order = $observer->getEvent()->getOrder();
$ShopatronExport=Mage::getModel('shopatron_orderexport/export');
$ShopatronExport->exportOrder($order);
return true;
}
}
And the Export Model is:
class Shopatron_OrderExport_Model_Export
{
public function exportOrder($order)
{
Mage::log('Shopaton Order exportOrder', null, 'Shopatron.log');
$dirPath = Mage::getBaseDir('var') . DS . 'export';
if (!is_dir($dirPath))
{
mkdir($dirPath, 0777, true);
}
$customerIP = Mage::helper('core/http')->getRemoteAddr(true);
//Getting the Proper 2 Character State
$regionModel = Mage::getSingleton('directory/region');
$regionModel->load($order->getShippingAddress()->getRegionId());
$sstate_code = $regionModel->getCode();
$regionModel->load($order->getBillingAddress()->getRegionId());
$bstate_code = $regionModel->getCode();
Mage::log($bstate_code, null, 'Shopatron.log');
Mage::log($sstate_code, null, 'Shopatron.log');
$data = array('OrderID'=>$order->getIncrementId(),
'Email'=>$order->getData('customer_email'),
'CustomerIP'=>$customerIP,
'ShipMethod' =>$order->getData('shipping_method'),
'ShippingPhone' =>$order->getShippingAddress()->getData('telephone'),
'ShippingFirstName'=>$order->getShippingAddress()->getFirstname(),
'ShippingLastName' => $order->getShippingAddress()->getLastname(),
'ShippingStreet' => $order->getShippingAddress()->getStreet(),
'ShippingCity' => $order->getShippingAddress()->getCity(),
'ShippingState' => $sstate_code,
'ShippingCountry' => $order->getShippingAddress()->getCountryId(),
'ShippingZip' => substr($order->getShippingAddress()->getPostcode(),0,5),
'BillingPhone' => $order->getBillingAddress()->getData('telephone'),
'BillingFirstName' => $order->getBillingAddress()->getFirstname(),
'BillingLastName' => $order->getBillingAddress()->getLastname(),
'BillingStreet' => $order->getBillingAddress()->getStreet(),
'BillingCity' => $order->getBillingAddress()->getcity(),
'BillingState' => $bstate_code,
'BillingCountry' => $order->getBillingAddress()->getCountryId(),
'BillingZip' => substr($order->getBillingAddress()->getPostcode(),0,5),
//Why can't I call getAllVisibleItems?
'Items' => $order->getAllVisibleItems()
);
try{
$data['Items']=createOrderItems( $order->getIncrementId());
} catch(Exception $e){
Mage::log($e->getMessage(), null, 'system.log');
//Nothing shows up in the log to help troubleshoot
}
//$xstrout = toXmlRpc($data);
/*file_put_contents(
$dirPath. DS .$data['OrderID'].'.xml22',
print "<PRE>" . htmlentities ( $xstrout ) . "</PRE>",
FILE_APPEND
);*/
//sendMsg($xstrout);
return true;
}
public function createOrderItems($orderId)
{
Mage::log('Shopatron Order createOrderItems', null, 'Shopatron.log');
$order=Mage::getModel('sales/order')->loadByIncrementId($orderId);
$items=$order->getAllVisibleItems();
$i=0;
$itemcount=count($items);
foreach ($items as $itemId => $item)
{
$IO[$itemId]['name']=$item->getName();
$IO[$itemId]['price']=$item->getPrice();
$IO[$itemId]['sku']=$item->getSku();
$IO[$itemId]['id']=$item->getProductId();
$IO[$itemId]['upc']=getUpcFromSku($item->getSku());
$IO[$itemId]['qty']=$item->getQtyOrdered(); //$item->getQtyToInvoice();
//$IO[$itemId]['options']=createOptionsArray($item->getProductOptions(), $itemId);
if($itemcount>1)
{
$IO[$itemId]['shipping']=$order->getData('shipping_amount')/$itemcount;
$IO[$itemId]['tax'] = $order->getData('tax_amount')/$itemcount;
}
elseif($itemcount == 1)
{
$IO[$itemId]['shipping']=$order->getData('shipping_amount');
$IO[$itemId]['tax'] = $order->getData('tax_amount');
}
$i++;
}
return $IO;
}
}
Any help would be much appreciated. Thanks!
Best Answer
It was some logic issue in your code
issue1:
'Items' => $order->getAllVisibleItems()
not works it is big object.it print and var_dump is stuck works.change the function
$data['Items']=$this->createOrderItems($order->getIncrementId());
and modified codeIssue:
lot of issue: