Magento – How to get Magento Invoice & shipment comment visible on frontend through SOAP API

magento-1.8magento-communityPHPsoap

I am writing a custom order handling script for magento CE. 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 also asked this question in stackoverflow. I hope thats OK.
If It get answered there, I'll post the answer here. Hope you guys can help me.
Thanks in advance.

Edit:
Ok , I found out this flag has to be set in database: is_visible_on_front . Is there anyway to pass this through the SOAP api? Of course I tried adding it to the call, as well as isVisibleOnFront but it doesn't work…

Best Answer

OK I found out how to do it, had to change a little code:

  1. I coppied /app/code/core/Mage/Sales/Model/Order/Api.php to /app/code/local/Mage/Sales/Model/Order/Api.php
  2. 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;
    }
    
  3. 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.

Related Topic