Jump to content

[SOLVED] What are the best hooks to use to add a js function when loading address form?


Recommended Posts

I have searched and come up with these two:

DisplayAdditionalCustomerAddressFields

ActionCheckoutRender

I think it is better to use:

ActionFrontControllerSetMedia

Because this way we can ensure libraries like jquery are loaded before loading any other library or code and just set an if clause to let it load the js file where you need it

Any other ideas?

Edited by Ali Samie (see edit history)
Link to comment
Share on other sites

  • 2 weeks later...
30 minutes ago, knacky said:

Thanks for this. I checked this and did not quite understand what it means.

When I was looking prestashop documents, I saw this on how to dispatch new event (?)

prestashop.emit(
  'myEventName',
  {
    myData1: 1,
    myData2: 3
  }
);

Then furthermore in the article I found this on how to define an event listener:

if (typeof prestashop !== 'undefined') {
  prestashop.on(
    'myEventName',
    function (event) {
      var eventDatas = {};
      if (event && event.reason) {
        eventDatas = {
          my_data_1: event.reason.myData1,
          my_data_2: event.reason.myData2
        };
      }
    }
  );
}

 

My question is how to trigger this event? I checked the link that you have shared and tried this:

prestashop.dispatchEvent('myEventName'); 

But it does not work and gives me this error:

Uncaught TypeError: prestashop.dispatchEvent is not a function

Link to comment
Share on other sites

well, it seems that I am really a noob when it comes to javascript.😅

prestashop.emit() works just like prestashop.dispatchEvent() should. Boy I wish I have never asked this silly question🤣

 

So to wrap things up, there are simply two steps:

1. add a event listener

2. trigger that event

 

for step one this is an example:

prestashop.on(
    'myEvent',
    function (event) {
        console.log('hello')
    }
);

for step two:

prestashop.emit('myEvent');

And for the result you should see hello in console

  • Like 1
Link to comment
Share on other sites

  • Ali Samie changed the title to [SOLVED] What are the best hooks to use to add a js function when loading address form?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...