Poll
What version of OpenCart do you use?


VoteShow Results

Simple guide to using Ajax in PrestaShop

Matt L. | Wednesday, 29 October 2014


I recently encountered a problem with the Ajax calls in our PrestaShop modules. The method we were using would not work at all if the store was set to maintenance mode, and for importing and exporting tools it is crucial that they work while the store is in maintenance mode. I discovered a work around for this issue, and it makes things pretty simple.

These steps are for our backoffice modules, and I have not tested them for frontend ajax calls but I assume they should work there too. (If anyone discovers otherwise then let me know and I will update this.)

 

Simple Ajax Handler

Step 1) Add an ajax handler function to your modules class file, the naming of this function is important. You need to prefix your method name with ajaxProcess.

In this case our Ajax handler is called MyAjaxMethod.

 

Step 2) Add your ajax call to one of your displayed pages

The URL for this call is important, it calls your main controller which would usually trigger a full page refresh, however if you specify ajax = true in the data parameters then your request is sent to the appropriate ajaxProcess function.

 

Done!

 

Sending dynamic data to your ajax handler

You won't always be working with simple static data, so to make your ajax handler more interactive you can simply pass it some variables with the data parameters of your ajax call.

Step 1) Edit your ajax handler to accept your data parameters

The PrestaShop function for getting post/get data also accepts an optional default value as the second parameter if the variable is not set.

 

Step 2) Add your desired variables to the ajax calls data parameters

 

...and that's it! Your ajax call will now prompt the user for their name, and respond with a personalized greeting.

 

Final Tips

Once you start writing more complex ajax calls, you will probably want to have your handler respond with json.

This is a very simple change, you just need to use json_encode on your ajax handlers output:

and modify your ajax call so it expects an ajax response by adding: dataType: 'json'

 

Easy! :)

That's all for now, get in touch if you have any comments about this guide.

v;ad
14/11/2016

skdflkshd;lakshf;laksflkasjf'asjfajsf;asjf;aojf
rtret
13/09/2016

retretret
ad
06/07/2016

sdfsdf
test
07/06/2016

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
vijay
18/02/2016

sefrgf
Luca
06/10/2015

After experimenting a while, I see that you can use the data obj for POST, while to activate the ajax methods you can simply pass the url like this url: [module_url] + '&ajax=true&action=ActionName'. The module url can be retrieve into a js global var from the template like this: {Link::getAdminLink("AdminModules")}&configure=nameofthemodule so the final url will be something like [admin_link]&ajax=true&configure=nameofthemodule&action=ActionName
Luca
28/09/2015

Thank you very very much for this tip!!!
Rein
15/03/2015

Im using your module Total Importer on prestashop 1.5.4.1 But it is not working over there. In the Console tab i got zero result. See my issue here : https://hostjars.zendesk.com/hc/en-us/requests/6377
Paul J
06/03/2015

Thank you so much for the article. It worked out! I had unsuccessfully tried to copy the "ajax-tab.php" to my root module and executed in order to process the "url" part. If you could explain why we can't use the regular ajax-tab.php instead the url string described, It would be very useful. Thank you
Unknown
14/01/2015

Can you explain more on the url. cause im having a hard time on the url part. thank you
Balazs
18/12/2014

Thank you very much!!!
dsfsadf
25/11/2014

asdfsadf

Write a comment

Your Name:


Your Comment: Note: HTML is not translated!

Enter the code in the box below



Phone Us: (408) 675-5884

Powered By OpenCart
HostJars © 2017