In my plugin I use the hook displayAdminOrder in this way:
public function hookDisplayAdminOrder($params)
{
$orderId = Tools::getValue('id_order'); // mi prendo l'id dell'ordine per le query sui docId
$order = new Order($orderId);
$order_status = $order->getCurrentOrderState();
//$order_status_name = $orderId;
if(is_object($order_status)){
if($order_status->name[1] == 'Annullato')
$order_status_name = '';
else
$order_status_name = $order_status->name[1];
}else{
$order_status_name = $order_status;
}
$query = 'SELECT docIdMyModule, docIdOrderMyModule, apiResponseOrder, apiResponseInvoice FROM '._DB_PREFIX_.'orders WHERE id_order=\'' . $orderId . '\';';
$docIdMyModule = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query)['docIdMyModule'];
$docIdOrderMyModule = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query)['docIdOrderMyModule'];
$apiResponseOrder = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query)['apiResponseOrder'];
$apiResponseInvoice = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query)['apiResponseInvoice'];
$json_apiResponseOrder = json_decode($apiResponseOrder);
$json_apiResponseInvoice = json_decode($apiResponseInvoice);
$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 ($docIdMyModule xor $docIdOrderMyModule) // incremento di uno se trovo uno dei due dati
$order_documents++;
else if ($docIdMyModule && $docIdOrderMyModule)
$order_documents = 2;
if(!$config_CreaOrdine){
$message_order = 'creazione documento disabilitata';
} else if(empty($order_status_name)){
$message_order = 'ordine annullato in Prestashop';
} else {
if(!empty($docIdOrderMyModule))
$message_order = 'documento salvato in MyModule';
else
$message_order = 'documento NON salvato in MyModule';
}
if(!$config_CreaFattura){
$message_invoice = 'creazione documento disabilitata';
} else if(empty($order_status_name)){
$message_invoice = 'ordine annullato in Prestashop';
} else {
if(!empty($docIdFattura24))
$message_invoice = 'documento salvato in MyModule';
else
$message_invoice = 'documento NON salvato in MyModule';
}
smartyRegisterFunction($this->context->smarty, 'function', 'saveOrder', array($this, 'smartyOrder')); // aggiungo la funzione per salvare di nuovo l'ordine
smartyRegisterFunction($this->context->smarty, 'function', 'saveInvoice', array($this, 'smartyInvoice')); // aggiungo la funzione per salvare di nuovo la fattura
$this->context->smarty->assign('id_order', $orderId);
$this->context->smarty->assign('order_status_name', $order_status_name); //se l'ordine è annullato nascondo i pulsanti
$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('mymodule_invoice_docId', $docIdMyModule); //docId per tasto fattura
$this->context->smarty->assign('mymodule_invoice', $message_invoice);
$this->context->smarty->assign('mymodule_order_docId', $docIdOrderMyModule); //docId per tasto ordine
$this->context->smarty->assign('mymodule_order', $message_order);
return $this->display(__FILE__, 'views/templates/hook/admin_order.tpl');
}
Here below admin_order.tpl :
<div class="panel" id="MyModule"> <div class="panel-heading"> <i class="icon-money"></i>{l s='MyModule' mod='mymodule'} <span class="badge">{$order_documents}</span> </div> <table> <tbody> <thead> <tr> <td><strong>{l s='Stato ordine' mod='mymodule'} </strong></td> <td style="padding-left: 20px;"><strong>{l s='Stato fattura' mod='mymodule'} </strong></td> </tr> </thead> <!--<tr> <td> Ordine : {$order_status_name} </td> <tr>--> <tr> <td id="f24_order">{$mymodule_order}</td> <td id="f24_invoice" style="padding-left: 20px;">{$mymodule_invoice}</td> </tr> <tr> <td> {if $order_status_name neq '' && $mymodule_order_docId == '' && $config_CreaOrdine eq 1} <button type="submit" name="submit_order" id="submit_order" class="btn btn-primary" onclick={saveOrder}>Salva Ordine</button> {/if} </td> <td style="padding-left: 20px;"> {if $order_status_name neq '' && $mymodule_invoice_docId == '' && $config_CreaFattura neq 0 } <button type="submit" name="submit_invoice" id="submit_invoice" class="btn btn-primary" onclick={saveInvoice}>Salva Fattura</button> {/if} </td> </tr> </tbody> </table> </div> </div> <script> var orderbtn = document.getElementById("submit_order"); if(orderbtn !== null){ orderbtn.addEventListener('click', function (e) { orderbtn.style.display = "none"; location.reload(); }); } var invoicebtn = document.getElementById("submit_invoice"); if(invoicebtn !== null){ invoicebtn.addEventListener('click', function (e) { invoicebtn.style.display = "none"; location.reload(); }); } </script>
In mymodule.php file I set up smartyOrder and smartyInvoice functions and I wish to execute them only when I click on the button. At the moment the functions are called also when I update an order.
Here below the smartyOrder function for example:
function smartyOrder($params){
$config_CreaOrdine = Configuration::get('PS_CREAZIONE_ORDINE');
$orderId = Tools::getValue('id_order'); // mi prendo l'id dell'ordine per le query sui docId
$check_order = new Order($orderId);
$order_status = $check_order->getCurrentOrderState(); // controllo lo stato dell'ordine prima di tutto
if(is_object($order_status)){
if($order_status->name[1] == 'Annullato')
$order_status_name = '';
else
$order_status_name = $order_status->name[1];
} else {
$order_status_name = $order_status;
}
$query = 'SELECT docIdMyModule, docIdOrderMyModule, apiResponseOrder, apiResponseInvoice FROM '._DB_PREFIX_.'orders WHERE id_order=\'' . $orderId . '\';';
$docIdOrderMyModule = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query)['docIdOrderMyModule'];
$apiResponseOrder = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query)['apiResponseOrder'];
//$apiResponseInvoice = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query)['apiResponseInvoice'];
$obj_apiResponseOrder = json_decode($apiResponseOrder);
//$obj_apiResponseInvoice = json_decode($apiResponseInvoice);
$this->afterHook($check_order, false);
if(!empty($docIdOrderMyModule)){
$query = 'UPDATE ' . _DB_PREFIX_ . 'orders SET docIdOrderMyModule=\'' . $docIdOrderMyModule . '\' WHERE id_order=\'' . $orderId . '\';';
Db::getInstance()->Execute($query);
//$this->downloadDocument($docIdOrderMyModule, $orderId, true);
} else if (!empty($obj_apiResponseOrder->description)) { // con questa condizione popolo il docId anche nel caso in cui il documento è già esistente in MyModule
$query = 'UPDATE ' . _DB_PREFIX_ . 'orders SET docIdOrderMyModule=\'' . $obj_apiResponseOrder->description . '\' WHERE id_order=\'' . $orderId . '\';';
Db::getInstance()->Execute($query);
}
}
Any suggestion? thank you very much