0

I'm trying to call a php event(magento event) 'payment_method_is_active' when one checkbox it is clicked. How can I do that?

enter image description here

3
  • stackoverflow.com/questions/6017558/… Commented Sep 20, 2016 at 8:17
  • Ok. but how do I say ajax to dispatch some php event? Commented Sep 20, 2016 at 8:21
  • Can't you put something like Mage::dispatchEvent('payment_method_is_active'... in a controller (which will be called via AJAX)? I do not know magento, but googled a bit Commented Sep 20, 2016 at 8:27

1 Answer 1

0

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>

You can send the ajax request to a controller and dispatch the event from there:

#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');
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.