Aliassse Posted March 18, 2013 Share Posted March 18, 2013 (edited) Hi ! I'm trying to call controller function through an AJAX call in Prestashop 1.5. I don't even know if it's possible. This is what I did : I override product controller (in override/controllers/front/ProductController.php) to load extra medias and to do some actions that the default controller does not do. This is what my controller looks like : <?php class ProductController extends ProductControllerCore { public function setMedia() { parent::setMedia(); // Add extra ressources // CSS & JS $this->addCSS(...) $this->addJS(array(...)); } // Extra methods public function renderCart() { echo '<h2>HELLO</h2>'; } } Here is my question: How can I call my renderCart() function through an AJAX call ? Is that even possible ? Edited March 18, 2013 by Aliassse (see edit history) Link to comment Share on other sites More sharing options...
supervacuum Posted May 9, 2013 Share Posted May 9, 2013 (edited) First point - it is possible to invoke a ModuleFrontController (or FrontController) using AJAX and have only your view rendered. Second point - unless you have a very specific reason to do so, I would recommend you to override the displayAjax() method. This will save you from specifying the controller method in the URI you will invoke. Steps: 1) Javascript - invoke the URL of the controller using the syntax you specified in the URLs and SEO section - normally defaults to /module/<modulename>/<controllername> and ensure you include the ajax=true parameter - this will instruct Prestashop to invoke the Ajax rendering and not the full page one. Example: $.ajax({ url: '{$base_dir}module/quickquote/thickness', type: 'get', data: 'ajax=true', success: function(data) { console.log('success'); $('#ajax').text(data); } }); Second, implement a controller and render a template using Smarty: public function displayAjax() { ... $this->smartyOutputContent($this->getTemplatePath() . 'foobar.tpl'); } You can also dump custom output and avoid Smarty like: die(output) For what concerns overriding the product controller, I cannot be of much help, never tried... Hope this helps. Edited May 9, 2013 by supervacuum (see edit history) Link to comment Share on other sites More sharing options...
manudas Posted May 29, 2014 Share Posted May 29, 2014 First point - it is possible to invoke a ModuleFrontController (or FrontController) using AJAX and have only your view rendered. Second point - unless you have a very specific reason to do so, I would recommend you to override the displayAjax() method. This will save you from specifying the controller method in the URI you will invoke. Steps: 1) Javascript - invoke the URL of the controller using the syntax you specified in the URLs and SEO section - normally defaults to /module/<modulename>/<controllername> and ensure you include the ajax=true parameter - this will instruct Prestashop to invoke the Ajax rendering and not the full page one. Example: $.ajax({ url: '{$base_dir}module/quickquote/thickness', type: 'get', data: 'ajax=true', success: function(data) { console.log('success'); $('#ajax').text(data); } });Second, implement a controller and render a template using Smarty: public function displayAjax() { ... $this->smartyOutputContent($this->getTemplatePath() . 'foobar.tpl'); } You can also dump custom output and avoid Smarty like: die(output)For what concerns overriding the product controller, I cannot be of much help, never tried... Hope this helps. I´m so sorry for reopening and old topic, but I think I need help with something similiar to this: How can I return via ajax response the result of $this->display(file, template) or $this->smartyOutputContent($templateStringFile) ? I mean... is there anyway of compiling/executing a theme file inside an ajax call and then returning the result via ajax response? Thanks a lot in advance. Manu Link to comment Share on other sites More sharing options...
manudas Posted May 29, 2014 Share Posted May 29, 2014 I´m so sorry for reopening and old topic, but I think I need help with something similiar to this: How can I return via ajax response the result of $this->display(file, template) or $this->smartyOutputContent($templateStringFile) ? I mean... is there anyway of compiling/executing a theme file inside an ajax call and then returning the result via ajax response? Thanks a lot in advance. Manu I´m happy to being able to answer myself and share the solution I found with the comunity. If you want to print the html code of the compilation of a template to a string, instead of printing to the screen, you can use the global smarty var this way: $output = $smarty->fetch($templateStringFile); Then, you can send this code in the ajax response with something like die($output) or echo $output. Hope I can help somebody with my findings. Greetings. Link to comment Share on other sites More sharing options...
studio-88 Posted October 9, 2014 Share Posted October 9, 2014 I have a problem I've been wrestling with tirelessly with no solution yet. I want to use ajax calls in my code on whether its a cms.tpl or shopping-cart.tpl to go to a seach result page and display a product info and its add to cart button. However my ajax calls get No 'Access-Control-Allow-Origin' responses. How can I remedy this problem? Can I do something to the controller files so that I can make ajax calls or is this method the wrong approach and I should just use php code to grab stuff from the database. EXAMPLE: $.ajax({ url: "{$base_dir}search?controller=search&orderby=position&orderway=desc&search_query=1+Year+Extended+Warranty+-+1+Year+Major+Appliance+Under+%24500+-+879520001572", type: "get", success: function(data){ $("#Warranty-Section-On-Check-Out-Page").append($(data).find("#center_column #product_list li")); but it doesn't work and this is what shows up in the console. XMLHttpRequest cannot load http://varoujappliances.com/search?controller=search&orderby=position&order…ear+Extended+Warranty+-+1+Year+Major+Appliance+Under+%24500+-+879520001572. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://varoujappliances.com' is therefore not allowed access. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now