I'm using Smarty in Prestashop. In my php file I built a function like this:
public function hookDisplayAdminOrderContentOrder($params)
{
$orderId = Tools::getValue('id_order'); // mi prendo l'id dell'ordine per le query sui docId
$query = 'SELECT docId1, docId1 FROM '._DB_PREFIX_.'orders WHERE id_order=\'' . $orderId . '\';';
$docId2 = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query)['docId2'];
$docId1 = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query)['docId1'];
$config_CreaOrdine = Configuration::get('PS_CREAZIONE_ORDINE');
$config_CreaFattura = Configuration::get('PS_CREAZIONE_FATTURA');
// uso order_documents per il contatore nel template
$order_documents = 0;
if ($docId1 || $docId2)
$order_documents++;
else if ($docId1 && $docId2)
$order_documents = 2;
if(!$docId1){
$message_order = 'documento NON salvato';
$submit_button_id = 'submit_order';
$submit_label = "Salva Ordine";
}else{
$message_order = 'documento salvato';
}
if(!$docId2){
$message_invoice = 'documento NON salvato';
$submit_button_id = 'submit_invoice';
$submit_label = "Salva Fattura";
}else {
$message_invoice = 'documento salvato';
}
smartyRegisterFunction($this->context->smarty, 'function', 'saveDocument', array($this, 'smartyDocument')); // aggiungo la funzione per salvare di nuovo l'ordine
$this->context->smarty->assign('id_order', $orderId);
$this->context->smarty->assign('config_CreaOrdine', $config_CreaOrdine);
$this->context->smarty->assign('config_CreaFattura', $config_CreaFattura);
$this->context->smarty->assign('order_documents', $order_documents); // contatore documenti salvati
$this->context->smarty->assign('order', $message_order);
$this->context->smarty->assign('order_docId', $docId1); //docId per tasto ordine
$this->context->smarty->assign('invoice', $message_invoice);
$this->context->smarty->assign('invoice_docId', $docId2); //docId per tasto fattura
return $this->display(__FILE__, 'views/templates/hook/admin_content_order.tpl');
}
Then in my admin_content_order.tpl I set the button name and id like this:
<div class="tab-pane" id="myplugin">
<table>
<tbody>
<thead>
<tr>
<td style="padding-left: 20px;"><strong>{l s='Stato ordine' mod='myplugin'} </strong></td>
<td style="padding-left: 20px;"><strong>{l s='Stato fattura' mod='myplugin'} </strong></td>
</tr>
</thead>
<tr>
<td style="padding-left: 20px;">{$order}</td>
<td style="padding-left: 20px;">{$invoice}</td>
</tr>
<tr>
<td style="padding-left: 20px;">
{if $order_docId == '' && $config_CreaOrdine eq 1}
<button type="submit" name="submit_order" id="submit_order" class="btn btn-primary" onclick={saveDocument}>Salva Ordine</button>
{/if}
</td>
<td style="padding-left: 20px;">
{if $invoice_docId == '' && !$config_CreaFattura eq 0}
<button type="submit" name="submit_invoice" id="submit_invoice" class="btn btn-primary" onclick={saveDocument}">Salva Fattura</button>
{/if}
</td>
</tr>
</tbody>
</table>
</div>
At last in my php file I define the function "smartyDocument". I wish a slight different behaviour of it, based on the button id. Anyone knows how I can I get the button id and pass it to "smartyDocument" function? I hope is all clear, thx
You cannot use any Smarty commands after loading the page. You can use the Javascript function instead.
Smarty is a server-side engine and the output is sent to the browser after processing, which is quite static.
You have to have a Javascript function to save your document by Ajax: