Reputation: 180
I have created an observer module that will send an email whenever a certain product is bought and paid for. In order to populate that email, I need to retrieve the Shipping Address, Billing Address, and Customer Name for a given order. Thus far my Observer.php looks like this:
<?php
class Electricjesus_Notifyowner_Model_Observer {
public function notifyOwnerEvent($observer) {
$invoice = $observer->getInvoice();
// derivative data
$order = $invoice->getOrder(); // Mage_Sales_Model_Order
$items = $order->getAllItems();
$i = 0;
foreach($items as $item) {
$skus[$i] = $item->getSku();
$i++;
}
$needle = 'train-01';
if( in_array( $needle, $skus ) ) {
ob_start();
var_dump($skus);
$skuDump = ob_get_clean();
$filedump = '$skuDump: ' . $skuDump . "\n" . '------------------------------' . "\n";
$shipAddr = $order->getBillingAddress();
ob_start();
var_dump($shipAddr);
$shipAddrDump = ob_get_clean();
$filedump .= '$shipAddrDump: ' . $shipAddrDump . "\n" . '------------------------------' . "\n";
$billAddr = $invoice->getShippingAddress();
ob_start();
var_dump($billAddr);
$billAddrDump = ob_get_clean();
$filedump .= '$billAddrDump: ' . $billAddrDump . "\n" . '------------------------------' . "\n";
$custName = $order->getCustomerName();
ob_start();
var_dump($custName);
$custNameDump = ob_get_clean();
$filedump .= '$custNamedump: ' . $custNameDump;
file_put_contents('observer_log.txt', $filedump);
$email = array(
'to' => '[email protected]',
'subject' => 'Confirmed Purchase',
'message' => "
<html>
<body>
". $custName ." has ordered and paid for the following products.
<table>
<th>
<td>
Items Ordered
</td>
<td>
Shipping Address
</td>
<td>
Billing Address
</td>
</th>
<tr>
<td>
". $skuDump ."
</td>
<td>
". $shipAddr ."
</td>
<td>
". $billAddr ."
</td>
</tr>
</table>
</body>
</html>",
'headers' => 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=iso-8859-1' . "\r\n" . 'From: Ashop.in Store'
);
//mail($email['to'], $email['subject'], $email['message'], $email['headers']);
}
return $this; // always return $this.
}
I have commented out the line to actually send the email preferring instead to simply output to a file as it's faster and easier given the large amount of information in the objects involved. I thought that $shipAddr = $order->getBillingAddress();
$billAddr = $invoice->getShippingAddress();
and $custName = $order->getCustomerName();
would be all I need but they return large objects rather than the simple strings I need.
How do I get the Addresses and Name output into simple strings that I can insert into an email?
Thank you in advance.
}
Upvotes: 0
Views: 326
Reputation: 11
You can get the email address like this:
'Email:' . print_r($order->getCustomerEmail(), true)
Upvotes: 0
Reputation: 17656
Try
$order->getBillingAddress()->format('html')
Also it would be better if you using the magento transactional email, take a look @ magento sending custom emails
Upvotes: 1