Reputation: 31
I am writing a custom order handling script for magento. If a new order is picked up by the script through cron it should create an invoice and notify the user and send a comment. I use the SOAP api for this purpose.
This works when sending an email, but how can I make the comment visible to the user on the frontend?
If I manually login to Magento admin, I can add a comment to an order and then check Visible on Frontend
.
I would like the comments I add with sales_order_invoice.create
and sales_order_shipment.create
to be visible to the customer in the Frontend the same way. I know that is not possible with default settings in the backend, but I would like to do that.
If this is really hard to do, I would at least like the comments added with sales_order.addComment
to be visible to the customer on the frontend just as when I manually comment and check Visible on Frontend
.
Here is my code for the SOAP proxy:
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
I know I can do some little improvements to this code, this was simply for testing the the soap API.
I learned to code PHP all by just reading guides on the internet and start trying. Stack overflow has been of great help in this journey for all the problems I encountered on my way; I have had a lot and I mean a lot of help by just using the search function on stack overflow in the past. Thanks for that! Of course I used it again together with big friend Google, but with no luck for the first time. That's why this is my first question here. I really hope you guys can help me out, thanks in advance!
Upvotes: 0
Views: 1165
Reputation: 31
OK I found out how to do it, had to change a little code:
/app/code/core/Mage/Sales/Model/Order/Api.php
to /app/code/local/Mage/Sales/Model/Order/Api.php
I changed the method 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;
}
And then in my 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);
}
Works like a charm! From what I've seen I can do the same thing in the shipment and invoice api files.
Upvotes: 1