使用 smarty 从模板获取元素 ID - Prestashop

get element id from template using smarty - Prestahop

我在 Prestashop 中使用 Smarty。在我的 php 文件中,我构建了一个这样的函数:

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');
    }

然后在我的 admin_content_order.tpl 中设置按钮名称和 ID,如下所示:

<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>

最后,在我的 php 文件中,我定义了函数 "smartyDocument"。根据按钮 ID,我希望它的行为略有不同。任何人都知道如何获取按钮 ID 并将其传递给 "smartyDocument" 函数?我希望一切都清楚,thx

加载页面后不能使用任何 Smarty 命令。您可以改用 Javascript 函数。

Smarty是服务器端引擎,处理后输出到浏览器,比较静态

您必须具有 Javascript 功能才能通过 Ajax 保存文档:

  1. 将您的数据发送到 PHP
  2. 保存数据
  3. return 成功或失败消息