从 smarty .tpl prestashop 调用 php 函数

call php function from smarty .tpl prestashop

我正在努力更新 Prestashop 的模块。在这里,我以这种方式使用 hookDisplayAdminOrder:

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();
    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 docId, docIdOrder, apiResponseOrder, apiResponseInvoice FROM '._DB_PREFIX_.'orders WHERE id_order=\'' . $orderId . '\';';
    $docId = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query)['docId'];   
    $docIdOrder = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query)['docIdOrder'];
    $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);
    $orderResponseDescription = is_object($obj_apiResponseOrder)? $obj_apiResponseOrder->description : $obj_apiResponseOrder['description'];
    $invoiceResponseDescription = is_object($obj_apiResponseInvoice)? $obj_apiResponseInvoice->description : $obj_apiResponseInvoice['description'];
    $config_CreaOrdine =  Configuration::get('PS_CREAZIONE_ORDINE');
    $config_CreaFattura =  Configuration::get('PS_CREAZIONE_FATTURA');

    if(!$config_CreaOrdine){
        $message_order = 'creazione documento disabilitata';
    } else if(empty($order_status_name)){
        $message_order = 'ordine annullato in Prestashop';
    } else {    
        if(!empty($docIdOrder))
            $message_order = 'documento salvato';
        else
            $message_order = 'documento NON salvato';
    }

    if(!$config_CreaFattura){
        $message_invoice = 'creazione documento disabilitata';
    } else if(empty($order_status_name)){
        $message_invoice = 'ordine annullato in Prestashop';    
    } else {
        if(!empty($docId))
            $message_invoice = 'documento salvato';
        else
            $message_invoice = 'documento NON salvato';
    }

    // uso order_documents per incrementare il contatore
    $order_documents = 0;
    if(strpos($message_order, 'documento salvato') !== false)
        $order_documents++;
    if(strpos($message_invoice, 'documento salvato') !== false)
        $order_documents++;  

    $this->context->smarty->assign('id_order', json_encode($order));
    $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('invoice_docId', $docId); //docId per tasto fattura
    $this->context->smarty->assign('invoice', $message_invoice);
    $this->context->smarty->assign('order_docId', $docIdOrder); //docId per tasto ordine
    $this->context->smarty->assign('order', $message_order);

    return $this->display(__FILE__, 'views/templates/hook/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 id="f24_order">{$order}</td>
                    <td id="f24_invoice" style="padding-left: 20px;">{$invoice}</td>
                </tr>

                <tr>
                    <td>
                        {if $order_status_name neq '' && $order_docId == '' && $config_CreaOrdine eq 1}  
                           <button type="submit" name="submit_order" id="submit_order" class="btn btn-primary">Salva Ordine</button>
                        {/if}
                    </td>

                    <td style="padding-left: 20px;">
                        {if $order_status_name neq '' && $invoice_docId == '' && $config_CreaFattura neq 0 } 
                            <button type="submit" name="submit_invoice" id="submit_invoice" class="btn btn-primary">Salva Fattura</button>
                        {/if}
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>    
<script type="text/javascript">
var orderbtn = document.getElementById("submit_order");

orderbtn.addEventListener('click', function (e) {
    $.ajax({
        type: 'POST',
        data: {
            ajax: true,
            action: 'SmartyOrder' + {$id_order},
        },
        url: 'myModule',
        success: function (response) {
           console.log(response);
           return response;
        }
    });
});

最终我在 myModule 中创建了 SmartyOrder 函数,但是当我点击我的按钮时它什么也没做。

方法如下:

function ajaxProcessSmartyOrder($params){
            $this->ajax = true;

            $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 docId, docIdOrder, apiResponseOrder, apiResponseInvoice FROM '._DB_PREFIX_.'orders WHERE id_order=\'' . $orderId . '\';';
            $docIdOrder = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query)['docIdOrder'];  
            $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($docIdOrder)){
                $query = 'UPDATE ' . _DB_PREFIX_ . 'orders SET docIdOrder=\'' . $docIdOrder . '\' WHERE id_order=\'' . $orderId . '\';';
                Db::getInstance()->Execute($query);
                //$this->downloadDocument($docIdOrder, $orderId, true);
            } else if (!empty($obj_apiResponseOrder->description)) { // con questa condizione popolo il docId anche nel caso in cui il documento è già esistente in F24
                $query = 'UPDATE ' . _DB_PREFIX_ . 'orders SET docIdOrder=\'' . $obj_apiResponseOrder->description . '\' WHERE id_order=\'' . $orderId . '\';';
                Db::getInstance()->Execute($query); 
            }

    }

我想从传递订单数据的模板中调用 SmartyOrder 函数,但目前我做不到。有什么建议吗?谢谢

您可以在名为“CarrierComparion”的 Prestashop 模块中找到有关您的问题的更多信息。您可以将当前代码与该模块的代码进行比较。

Post 表格使用 (Ajax): https://github.com/PrestaShop/ps_carriercomparison/blob/master/js/carriercompare.js#L84

正在接收 post (PHP): https://github.com/PrestaShop/ps_carriercomparison/blob/master/controllers/front/carrier_comparison.php#L45