简体   繁体   English

Magento 1.7.0.2 Oberserver模块

[英]Magento 1.7.0.2 Oberserver Module

I am attempting to create an observer module based on this answer. 我正在尝试根据此答案创建观察者模块 The end result is to send an email when a specific product has been purchased and payment has been received. 最终结果是在购买特定产品并收到付款后发送电子邮件。 I hope to accomplish this by setting the observer to trigger when an order is completed. 我希望通过将观察者设置为在订单完成时触发来完成此操作。 Thus far, I'm implemented the observer based on what I'd read in the above link and asked it to email me with the order info. 到目前为止,我已经根据我在上面的链接中阅读的内容实现了观察者,并要求它向我发送订购信息。 So far it does absolutely nothing. 到目前为止,它绝对没有任何作用。 Am I doing something wrong? 难道我做错了什么? the only modification I've made to what was provided is as follows: 我对提供的内容所做的唯一修改如下:

/app/code/local/Electricjesus/Notifyowner/Model/Observer.php /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.
    }

}

I've also tried writing the relevant info to a file using file_put_contents('observer_log.txt', '$order = '.$order); 我也尝试过使用file_put_contents('observer_log.txt', '$order = '.$order);将相关信息file_put_contents('observer_log.txt', '$order = '.$order);文件file_put_contents('observer_log.txt', '$order = '.$order); to no effect. 没有任何作用。 Any advice at all would be appreciated. 任何建议都将不胜感激。 I apologize for my noobishness. 我为我的笨拙而道歉。

EDIT: My config.xml [updated] file is as follows: 编辑:我的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>

EDIT 2: I have since modified my config.xml It used to be 编辑2:自从我修改了config.xml以来,

<events>
                    <sales_order_payment_pay>
                        <observers>

I changed the event to what you see above, though all the variables appear empty so I can't see if the order contains the product I care about before sending the email. 我将事件更改为您在上面看到的内容,尽管所有变量都显示为空,所以在发送电子邮件之前,我看不到订单中是否包含我所关心的产品。

I finally got it figured out. 我终于明白了。 For reasons I cannot begin to fathom, the documentation completely omits the information I wanted to know. 由于某些原因,我无法开始理解,因此文档完全省略了我想知道的信息。

At any rate, in order to make an observer module that will send an email when a certain product is purchased and invoiced, you need the following: 无论如何,为了使观察者模块能够在购买某种产品并开具发票后发送电子邮件,您需要以下内容:

/app/etc/modules/Electricjesus_Notifyowner.xml /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 /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>

and finally, /app/code/local/Electricjesus/Notifyowner/Model/Observer.php 最后是/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.
    }

}

Note that the product being detected is specified via $needle = 'mysku-01'; 请注意,通过$needle = 'mysku-01';指定检测到的产品$needle = 'mysku-01'; and the email itself is built via the $email[] array. 电子邮件本身是通过$email[]数组构建的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM