Magento 1.7.0.2 Oberserver Module

时间:2013-11-29 18:27:26

标签: php magento email

我正在尝试根据this answer.创建一个观察者模块。最终结果是在购买特定产品并收到付款后发送电子邮件。我希望通过将观察者设置为在订单完成时触发来实现此目的。 到目前为止,我根据上面链接中的内容实现了观察者,并要求它通过电子邮件向我发送订单信息。到目前为止它绝对没有。难道我做错了什么?我对所提供的内容进行的唯一修改如下:

/app/code/local/Electricjesus/Notifyowner/Model/Observer.php

<?php
class Electricjesus_Notifyowner_Model_Observer
{
    public function notifyOwnerEvent($observer)
    {

        // parameters you can get from the $observer parameter:
        // array(’payment’ ? $this, ‘invoice’ ? $invoice)

        $payment = $observer->getPayment();
        $invoice = $observer->getInvoice();

        // derivative data
        $order = $invoice->getOrder(); // Mage_Sales_Model_Order

        $email = array(
            'to' => 'me@mydomain.com',
            'subject' => 'Confirmed Purchase',
            'message' => 'Hello World. This is a test. $order = ' . $order,
            'headers' => 'From: My Store'
        ); 

        mail($email['to'], $email['subject'], $email['subject'], $email['headers']);

        /*
             - build data
             - build email structure
             - send email via any php mailer method you want
        */
        return $this;  // always return $this.
    }

}

我还尝试使用file_put_contents('observer_log.txt', '$order = '.$order);将相关信息写入文件,但效果不佳。任何建议都将不胜感激。我为我的无聊而道歉。

编辑:我的config.xml [更新]文件如下:

<?xml version="1.0"?>
<config>
    <modules>
        <Electricjesus_Notifyowner>
                <version>0.1.0</version>
        </Electricjesus_Notifyowner>
    </modules>
    <global>
        <models>
            <notifyowner>
                <class>Electricjesus_Notifyowner_Model</class>
            </notifyowner>
        </models>          
        <events>
                <sales_order_invoice_pay>
                    <observers>
                        <notifyOwnerEvent>
                                <class>notifyowner/observer</class>
                                <method>notifyOwnerEvent</method>
                        </notifyOwnerEvent>
                    </observers>
                </sales_order_invoice_pay>     
        </events>
     </global>
</config>

编辑2:我已经修改了我的config.xml以前是

<events>
                    <sales_order_payment_pay>
                        <observers>

我将事件更改为您在上面看到的内容,但所有变量都显示为空,因此在发送电子邮件之前,我无法查看订单是否包含我关注的产品。

1 个答案:

答案 0 :(得分:0)

我终于明白了。由于我无法理解的原因,文档完全省略了我想知道的信息。

无论如何,为了制作一个观察员模块,当购买某个产品并开具发票时,该模块将发送电子邮件,您需要以下内容:

/app/etc/modules/Electricjesus_Notifyowner.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Electricjesus_Notifyowner>
            <active>true</active>
            <codePool>local</codePool>
        </Electricjesus_Notifyowner>
    </modules>
</config>

/app/code/local/Electricjesus/Notifyowner/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Electricjesus_Notifyowner>
                <version>0.1.0</version>
        </Electricjesus_Notifyowner>
    </modules>
    <global>
        <models>
            <notifyowner>
                <class>Electricjesus_Notifyowner_Model</class>
            </notifyowner>
        </models>          
        <events>
                <sales_order_invoice_pay>
                    <observers>
                        <notifyOwnerEvent>
                                <class>notifyowner/observer</class>
                                <method>notifyOwnerEvent</method>
                        </notifyOwnerEvent>
                    </observers>
                </sales_order_invoice_pay>     
        </events>
     </global>
</config>

最后是/app/code/local/Electricjesus/Notifyowner/Model/Observer.php

<?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) {
            $str .= $item->getSku() . "/n";
            $skus[$i] = $item->getSku();
            $i++;
        }

        $shipAddr = $order->getBillingAddress();
        $billAddr = $order->getShippingAddress();
        $custName = $order->getCustomerName();

        $email = array(
            'to' => 'myname@maydomain.com',
            '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>
                                    '. $str .'
                                </td>
                                <td>
                                    '. $shipAddr .'
                                </td>
                                <td>
                                    '. $billAddr .'
                                </td>
                            </tr>
                        </table>
                    </body>
                </html>',
            'headers' => 'From: My Store'
        ); 

        $needle = 'mysku-01';

        if( in_array( $needle, $skus ) ) {
            mail($email['to'], $email['subject'], $email['message'], $email['headers']);
        }

        return $this;  // always return $this.
    }

}

请注意,检测到的产品是通过$needle = 'mysku-01';指定的,电子邮件本身是通过$email[]数组构建的。