我正在为magento编写一个自定义订单处理脚本。如果脚本通过cron获取了一个新订单,它应该创建一个发票,并通知用户并发送评论。为此,我使用SOAP api。这在发送电子邮件时是有效的,但是我如何使评论对前端的用户可见呢?如果我手动登录Magento admin,我可以给订单添加评论,然后检查Visible on Frontend
。我希望我用sales_order_invoice.create
和sales_order_shipment.create
添加的评论以同样的方式对前端的客户可见。我知道这是不可能的默认设置后端,但我想这样做。如果这真的很难做到,我至少希望用sales_order.addComment
添加的评论对前端的客户是可见的,就像我手动注释和检查Visible on Frontend
一样。
class magentoProxyHandler{
protected $proxy;
protected $session;
function __construct(){
$this->proxy = new SoapClient('http://www.magento.nl/index.php/api/soap/?wsdl');
$this->session = $this->proxy->login('change_order', 'password');
}
function __destruct(){
$this->proxy->endSession($this->session);
}
function addComment($orderId, $status, $comment = '', $notifyCustomer = true){
$orderId = ($orderId > 100000000 ? $orderId : $orderId + 100000000);
$notify = $notifyCustomer ? true : false;
$changeOrder = array('orderIncrementId' => $orderId, 'status' => $status, 'comment'=> $comment, 'notify'=> $notify);
return $this->proxy->call($this->session, 'sales_order.addComment', $changeOrder);
}
function createInvoice($orderId, $status, $comment = 'Invoice ready', $notifyCustomer = true){
$orderId = ($orderId > 100000000 ? $orderId : $orderId + 100000000);
$notify = $notifyCustomer ? true : false;
return $this->proxy->call($this->session, 'sales_order_invoice.create', array($orderId, array(), $comment, true, true));
}
function shipOrder($orderId, $status, $comment = 'Order shipped', $notifyCustomer = true){
$orderId = ($orderId > 100000000 ? $orderId : $orderId + 100000000);
$notify = $notifyCustomer ? true : false;
return $this->proxy->call($this->session, 'sales_order_shipment.create', array($orderId, array(), $comment, true, true));
}
} //end of class
我知道我可以对这段代码做一些小小的改进,这只是为了测试soap API。
我学会编写PHP代码只是通过阅读指南在互联网上,并开始尝试。对于我在旅途中遇到的所有问题,Stack overflow都提供了很大的帮助;我有很多,我的意思是很多的帮助,通过使用搜索功能在堆栈溢出在过去。谢谢你!当然,我又和我的好朋友谷歌一起使用了它,但第一次没能成功。这就是为什么这是我的第一个问题。我真的希望你们能帮助我,提前感谢!
好的,我知道怎么做了,必须修改一点代码:
- 我复制
/app/code/core/Mage/Sales/Model/Order/Api.php
到/app/code/local/Mage/Sales/Model/Order/Api.php
-
我改变了方法addComment:
public function addComment($orderIncrementId, $status, $comment = '', $notify = false, $showOnFront = true) { $order = $this->_initOrder($orderIncrementId); $historyItem = $order->addStatusHistoryComment($comment, $status); $historyItem->setIsVisibleOnFront($showOnFront); $historyItem->setIsCustomerNotified($notify)->save(); try { if ($notify && $comment) { $oldStore = Mage::getDesign()->getStore(); $oldArea = Mage::getDesign()->getArea(); Mage::getDesign()->setStore($order->getStoreId()); Mage::getDesign()->setArea('frontend'); } $order->save(); $order->sendOrderUpdateEmail($notify, $comment); if ($notify && $comment) { Mage::getDesign()->setStore($oldStore); Mage::getDesign()->setArea($oldArea); } } catch (Mage_Core_Exception $e) { $this->_fault('status_not_changed', $e->getMessage()); } return true; }
-
然后在我的class中:
function addComment($orderId, $status, $comment = '', $notifyCustomer = true, $showOnFront = true){ $orderId = ($orderId > 100000000 ? $orderId : $orderId + 100000000); $notify = $notifyCustomer ? true : false; $changeOrder = array('orderIncrementId' => $orderId, 'status' => $status, 'comment'=> $comment, 'notify'=> $notify, 'showOnFront' => $showOnFront); return $this->proxy->call($this->session, 'sales_order.addComment', $changeOrder); }
工作像一个魅力!根据我所看到的,我可以在装运和发票api文件中做同样的事情。