doudun2565
2013-12-06 16:05
浏览 35
已采纳

Magento 1.7.0.2获取订单信息

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 . "
" . '------------------------------' . "
";

            $shipAddr = $order->getBillingAddress();
            ob_start();
            var_dump($shipAddr);
            $shipAddrDump = ob_get_clean();
            $filedump .= '$shipAddrDump: ' . $shipAddrDump . "
" . '------------------------------' . "
";

            $billAddr = $invoice->getShippingAddress();
            ob_start();
            var_dump($billAddr);
            $billAddrDump = ob_get_clean();
            $filedump .= '$billAddrDump: ' . $billAddrDump . "
" . '------------------------------' . "
";

            $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' => 'daniel@danielclarke.ca',
                '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' . "
" . 'Content-type: text/html; charset=iso-8859-1' . "
" . '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.

}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsgdhf5674 2013-12-06 17:12
    已采纳

    Try

    $order->getBillingAddress()->format('html')

    Also it would be better if you using the magento transactional email, take a look @ magento sending custom emails

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douqi0090 2013-12-12 05:48

    You can get the email address like this:

    'Email:' . print_r($order->getCustomerEmail(), true)
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题