1

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...

1

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.

  • 1
    So, first of all: You copied the whole class to app/code/local for changing two lines. That's not very update-friendly. A rewrite with only the needed functin included would me more transparent. Second, you did not make comments about the changes in the file so I looked it up: you added one parameter $showOnFront = true and added the line $historyItem->setIsVisibleOnFront($showOnFront); – Anna Völkl Aug 1 '14 at 21:20
  • Thanks for your comment. Yes indeed I did. How would you suggest I rewrite only that method? I don't know how to do that... Regarding the changes I made, I guess I should have just stated which lines to change. I'll remember that for next time. – 076 Aug 3 '14 at 13:09
  • 1
    You can just make a new class that inherits the old one, then only change the method(s) you need to. This link may help: inchoo.net/magento/… Or, search around for "magento overriding classes", there are other tutorials and articles out there. – Mike May 1 '15 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.