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
'TEST FROM INDEXACTION'
) is only a test and is going to change to something else. But as you can see the properstatus
is not modified/updated