0

I have a magento event 'payment_method_is_active' and I want to call event with ajax.How can I do?

UPDATE: I have an Observer :

<frontend>
        <events>
            <payment_method_is_active>
                <observers>
                  <some_observer>
                        <class>namespace/observer</class>
                        <method>checkObserver</method>
                   </some_observer>
                </observers>
            </payment_method_is_active>
        </events>
</frontend>

I want that event to be dispatch everytime I click in one field. for that I have an ajax function :

 jQuery.ajax({
  method: "POST",
  url: "/some/url"}).done(function() {
     jQuery('#fieldId').click(function(){
        //Here I want to dispatch 'payment_method_is_active' event
    });
});
  • could you please add more information, your question is not clear – Rickert Sep 20 '16 at 13:15
  • Check my update. – ana.coman93 Sep 20 '16 at 13:25
  • may i ask why you want this? – Rickert Sep 20 '16 at 14:12
5

You need to make ajax call to a custom controller action and do the dispatch event there as below:

In app/etc/modules/MyCompany_Ajax.xml

<config>
  <modules>
    <MyCompany_Ajax>
      <active>true</active>
      <codePool>local</codePool>
    </MyCompany_Ajax>
  </modules>
</config>

In app/etc/code/local/MyCompany/Ajax/etc/config.xml

<config>
    <modules>
        <MyCompany_Ajax>
            <version>1.0.0</version>
        </MyCompany_Ajax>
    </modules>
    <global>
        <frontend>
            <routers>
                <mycompanyajax>
                    <use>standard</use>
                    <args>
                        <module>MyCompany_Ajax</module>
                        <frontName>mycompanyajax</frontName>
                    </args>
                </mycompanyajax>
            </routers>
        </frontend>
    </global>
</config>

In app/code/local/MyCompany/Ajax/controllers/AjaxController.php

<?php
class MyCompany_Ajax_AjaxController extends Mage_Core_Controller_Front_Action
{
    public function paymentAction()
    {
        $paymentMethodStatus = $this->getRequest()->getParam('is_active', false);
        //The $args variable may differ based on what values you are expecting in your observer.
        $args = array ('is_active', $paymentMethodStatus);
        Mage::dispatchEvent('payment_method_is_active', $args);
    }
}

In you template file:

$("#paymentCheckBox").live("click", function(){
    var isChecked = $(this).is(":checked");
    $.ajax({
        type: "POST",
        url: mycompanyajax/ajax/payment,
        data: { 'is_active' : isChecked},
        success: function(data) {
            //Handle if you return something
        },
        error: function() {
            alert('Something went wrong');
        },
        complete: function() {
            alert('Its done');
        }
    });
});
| improve this answer | |

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.