0

I'm working on a custom module for Magento CE 1.9.1 and i need to add a comment to an order after a javascript function is triggered (clicking on a button). I thought to do this using an AJAX/XMLHTTPRequest call, pointing to a php file that has a code similar to this.

From what I understood I can't use a general php file like I thought, but instead I need to use a Controller (tell me if I'm wrong).

I created a basic controller on the module I was working, applying those changes:

/app/code/local/test/testModule/etc/config.xml

<config>
    <!--...-->
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <test_testModule after="Mage_Adminhtml">test_testModule</test_testModule>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <!--...-->
</config>

/app/code/local/test/testModule/controllers/IndexController.php

<?php
class test_testModule_IndexController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }


}  

And nothing else. All the other code hasn't been touched. To call the controller I followed the answer given by Louis Bataillard on this thread, so

Mage::helper('adminhtml')->getUrl('adminhtml/testModule/index/');

I know the controller by now shouldn't really do anything, but I keep getting an error 302 which should be wrong.

Do you have any tip or a better idea about how to do this?

Edit: I've managed to make the controller work, modifying the module name from testModule to testmodule and the controller name from IndexController to testmoduleController.

Now in my Javascript I have

<script>
/* ... */
    var tempPath = "<?php echo Mage::helper('adminhtml')->getUrl('adminhtml/testmodule/index/'); ?>"+"?isAjax=true";
    var addcomment = new XMLHttpRequest();
    var commentParameters = "orderID="+<?php echo $_order->getId(); ?>+"&supplier="+chosenSupplier;
    addcomment.open("post", tempPath, true);
    addcomment.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    addcomment.send(commentParameters);
</script>

And in my controller, into indexAction

    public function indexAction()
            {
            //$orderID=$_POST['orderID'];
            //$supplier=$_POST['supplier'];
            $orderID=4882;
            $_order = Mage::getModel('sales/order')->load($orderID);
            $_order->addStatusToHistory($_order->getStatus(), 'TEST FROM INDEXACTION, false);
            $_order->save();
    }

As you can see right now I'm loading a static order, but I the code to get the content I need is the one on the two commented rows above.

Now when I call the controller from the javascript I get an answer 200 but nothing happens , meanwhile when I call directly the controller from the URL it works and adds a comment. Any idea about what is the problem here?

Note: I get the 200 only because I added the ?isAjax=true, otherwise I would still get a 302.

Edit2: I'm not sure this is the appropriate way to do it, but I found a workaround.

I've passed from a POST to a GET. This way I have to pass the parameters into the url, so this is how I modified the javascript:

<script>
    /* ... */
    var tempPath= "<?php echo Mage::helper('adminhtml')->getUrl('adminhtml/helloworld/comment/'); ?>"+"?isAjax=true"+"&orderID="+<?php echo $_order->getId(); ?>+"&supplier="+chosenSupplier;
    var addcomment = new XMLHttpRequest();
    addcomment.open("get", tempPath, true);
    addcomment.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    addcomment.send();
</script>

And, into the testmoduleController, I've modified this:

    public function indexAction()
{
    $orderID=$_GET['orderID'];
    $supplier=$_GET['supplier'];
    $_order = Mage::getModel('sales/order')->load($orderID);
    $_order->addStatusToHistory($_order->getStatus(), 'TEST FROM INDEXACTION; supplier:'.$supplier, false);
    $_order->save();
}

I'm leaving the question open, so you can tell me if there is another (a better) way to do this

  • what kind of comment u want to add? is it like updating an status? – Mujahidh Aug 30 '15 at 12:06
  • @mujas something similar. I need to log the supplier I take from the XMLHttpRequest... The string behind (in the code above 'TEST FROM INDEXACTION') is only a test and is going to change to something else. But as you can see the proper status is not modified/updated – tabris963 Aug 30 '15 at 13:32
  • fine, I guess that you want to update the order status in some condition and suggest you an answer by using observer.Please try to get some idea from the answer. – Mujahidh Aug 30 '15 at 13:37
0

You have to create your own custom module.I guess you have good idea about creating an module and observer and event handling.

Config.xml file Path :- app\code\local\Wired\Orders\etc

<?xml version="1.0"?>
<config>
<modules>
    <Wired_Orders>
        <version>0.1.0</version>
    </Wired_Orders>
</modules>
<global>
    <models>
        <orders>
            <class>Wired_Orders_Model</class>
        </orders>
    </models>
    <helpers>
        <orders>
            <class>Wired_Orders_Helper</class>
        </orders>
    </helpers>
    <events>
        <checkout_submit_all_after>
            <observers>
                <change_order_status>
                    <class>orders/observer</class>
                    <method>CodeOrdeStatus</method>
                </change_order_status>
            </observers>
        </checkout_submit_all_after>
    </events>
 </global>   

Observer.php file Path :- app\code\local\Wired\Orders\Model

<?php
class Wired_Orders_Model_Observer extends Varien_Event_Observer
{
   public function CodeOrdeStatus($observer){

     $order = $observer->getEvent()->getOrder();
     $order=Mage::getModel('sales/order')->load($order->getId());
     // what ever the condition you used to update the comment
     if(**you condition**):

     //Method 1
     $order->setStatus("**Your Status**");//What ever the text you want to update with order status.
           //OR you can use like this

     //Method 2
      $order->addStatusToHistory('complete', 'Put your comment here', false);
     $order->save(); 
     endif;
}
}

module xml(Wired_Orders.xml) file Path : - \app\etc\modules

<?xml version="1.0" encoding="utf-8"?>
<config>
<modules>
    <Wired_Orders>
        <active>true</active>
        <codePool>local</codePool>
    </Wired_Orders>
</modules>

Hope you can get an idea from this coding part and customize your code accodring to your need.

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.