Magento - 从观察者事件添加订单注释

时间:2013-09-20 19:08:57

标签: magento comments audit

我一直在努力争取自己,希望有人可以帮助我。

我有一些观察员事件完美地运作,它只是一个似乎不起作用的事件。

creditmemoRefund函数不会在订单中添加注释。订单2工作正常,它肯定是按照预期发射,并拉动订单对象。为什么不添加评论?

这是我的代码......

config.xml中

<?xml version="1.0"?>
<config>
  <modules>
    <Avi_OrderAudit>
      <version>0.0.1</version>
    </Avi_OrderAudit>
  </modules>
  <global>
<helpers>
  <orderaudit>
    <class>Avi_OrderAudit_Helper</class>
  </orderaudit>
</helpers>
<models>
  <orderaudit>
    <class>Avi_OrderAudit_Model</class>
    <resourceModel>orderaudit_mysql4</resourceModel>
  </orderaudit>
</models>
<events>
  <sales_order_place_after> <!-- identifier of the event we want to catch -->
    <observers>
      <sales_order_place_after_handler> <!-- identifier of the event handler -->
        <type>model</type> <!-- class method call type; valid are model, object and singleton -->
        <class>orderaudit/observer</class> <!-- observers class alias -->
        <method>orderPlaced</method>  <!-- observer's method to be called -->
        <args></args> <!-- additional arguments passed to observer -->
      </sales_order_place_after_handler>
    </observers>
  </sales_order_place_after>
  <sales_order_payment_capture> <!-- identifier of the event we want to catch -->
    <observers>
      <sales_order_payment_capture_handler> <!-- identifier of the event handler -->
        <type>model</type> <!-- class method call type; valid are model, object and singleton -->
        <class>orderaudit/observer</class> <!-- observers class alias -->
        <method>paymentCapture</method>  <!-- observer's method to be called -->
        <args></args> <!-- additional arguments passed to observer -->
      </sales_order_payment_capture_handler>
    </observers>
  </sales_order_payment_capture>
  <sales_order_creditmemo_refund> <!-- identifier of the event we want to catch -->
    <observers>
      <sales_order_creditmemo_refund_handler> <!-- identifier of the event handler -->
        <type>model</type> <!-- class method call type; valid are model, object and singleton -->
        <class>orderaudit/observer</class> <!-- observers class alias -->
        <method>creditmemoRefund</method>  <!-- observer's method to be called -->
        <args></args> <!-- additional arguments passed to observer -->
      </sales_order_creditmemo_refund_handler>
    </observers>
  </sales_order_creditmemo_refund>
</events>

Observer.php     class Avi_OrderAudit_Model_Observer     {

    public function orderPlaced(Varien_Event_Observer $observer)
    {
        if (Mage::getSingleton('admin/session')->isLoggedIn()) {
        //if admin
            $order = $observer->getEvent()->getOrder();
            $user = Mage::getSingleton('admin/session');
            $username = $user->getUser()->getUsername();
            $comment = "Order placed by <strong>".$username."</strong>";
            $order->addStatusHistoryComment($comment)
                ->setIsVisibleOnFront(false)
                ->setIsCustomerNotified(false);
        }
        else {
        //placed by customer online
            $order = $observer->getEvent()->getOrder();
            $order->addStatusHistoryComment('Order placed online by customer')
                ->setIsVisibleOnFront(false)
                ->setIsCustomerNotified(false);
        }
    }

    public function paymentCapture(Varien_Event_Observer $observer)
    {
        $invoice = $observer->getEvent()->getInvoice();
        $order = $invoice->getOrder();
        $user = Mage::getSingleton('admin/session');
        $username = $user->getUser()->getUsername();
        $comment = "Payment captured by <strong>".$username."</strong>";
        $order->addStatusHistoryComment($comment)
            ->setIsVisibleOnFront(false)
            ->setIsCustomerNotified(false);
    }
    public function creditmemoRefund(Varien_Event_Observer $observer)
    {
        $creditmemo = $observer->getEvent()->getCreditmemo();
        $order = $creditmemo->getOrder();
        $user = Mage::getSingleton('admin/session');
        $username = $user->getUser()->getUsername();
        $comment = "Refund by <strong>".$username."</strong>";
        $order->addStatusHistoryComment($comment)
            ->setIsVisibleOnFront(false)
            ->setIsCustomerNotified(false);

    }
}

1 个答案:

答案 0 :(得分:2)

之后

$order->addStatusHistoryComment($comment)
            ->setIsVisibleOnFront(false)
            ->setIsCustomerNotified(false);

你需要

$order->save();