Magento 1.7.0.2上级服务器模块



我正试图基于这个答案创建一个观察者模块。最终结果是在购买了特定产品并收到付款后发送电子邮件。我希望通过将观察者设置为在订单完成时触发来实现这一点。到目前为止,我已经根据我在上面链接中读到的内容实现了观察者,并要求它向我发送订单信息。到目前为止,它什么都没做。我做错什么了吗?我对提供的内容所做的唯一修改如下:

/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>

第二版:我修改了我的config.xml,它曾经是

<events>
                    <sales_order_payment_pay>
                        <observers>

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

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

无论如何,为了制作一个观察者模块,在购买和开具特定产品发票时发送电子邮件,您需要以下内容:

/app/etc/modules/Electricesus_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[]数组构建的。

最新更新