Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

482
Visualizações
Call a prestashop module with ajax

I have a PrestaShop module called 'MyMenu' and I want call this menu with an AJAX call. My module is displayed in the hookFooter() method:

public function hookFooter()
{
    $display = $this->display(__FILE__, 'megamenu.tpl', $smartyCacheId);
    Tools::restoreCacheSettings();
    return  $display;
}

I want display with this script:

<div class="load_menu"></div>
<script>
    $(document).ready(function (e) {
        $.ajax({
            method: "POST",
            url: "../modules/MyMenu.php",
            data: {},
            success: function (data) {
                $('.load_menu').html(data);
            }
        })
    });
</script>
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

The best way is to do it via a front controller linked to your module. You can call the url like this :

$link->getModuleLink('modulename','controller', $parameters);
// Parameters is an optionnal array, it can be empty

And for the controller, place a file like this ./modules/modulename/controllers/front/ajax.php with this kind of content :

class ModuleNameAjaxModuleFrontController extends ModuleFrontController
{
public function initContent()
{

    $response = array('status' => false);

    require_once _PS_MODULE_DIR_.'modulename/modulename.php';

    $module = new ModuleName;

    if (Tools::isSubmit('action')) {
        $context = Context::getContext();

        $cart = $context->cart;

        switch (Tools::getValue('action')) {

            case 'actionname':


                $response = array('status' => true);

                break;

            default:
                break;

        }
    }

    // Classic json response
    $json = Tools::jsonEncode($response);
    $this->ajaxDie($json);

    // For displaying like any other use this method to assign and display your template placed in modules/modulename/views/template/front/...
    // $this->context->smarty->assign(array('var1'=>'value1'));
    // $this->setTemplate('template.tpl');

    // For sending a template in ajax use this method
    // $this->context->smarty->fetch('template.tpl');

}
}
about 4 years ago · Santiago Trujillo Relatório

0

If you don't want to pass the url by the module, the js snippet should be like this.

$(document).ready(function(){
    $.ajax({
        type: "POST", 
        headers: { "cache-control": "no-cache" },
        url : baseDir + 'modules/yourmodulename/yourfile.php',
        data: {
            token : token
        },
        success : function(data){
            $('.load-menu').html(data)
        }
    });
});

Where yourmodulename is the name of your module and yourfile.php is the code where you retrieve the menu.

Don't forget to add to your data the token, it's to prevent a CSFR attack, obviously you have to check the token in your server side script as well.

about 4 years ago · Santiago Trujillo Relatório

0

In a new file at the module root, you can create a file "ajax.php"

require_once(MODULE_DIR.'MyMenu/mymenu.php');
if(Tools::getValue('token') !=
$mymenu = Module::getInstanceByName('mymenu');
$menu = $mymenu->hookFooter();
die($menu);

In your js, at the root of your module

<script>
$(document).ready(function (e) {
    $.ajax({
        method: "POST",
        url: "./ajax.php",
        data: {},
        success: function (data) {
            $('.load_menu').html(data);
        }
    })
});
</script>
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda