Kingshuk Deb
Kingshuk Deb

Reputation: 1720

how to add php code in .tpl file in prestashop

Does anyone know how to add php code in .tpl file in prestashop..i tried a lot but couldn't find any solutions..I want to add a mail function in my .tpl file

This is the function

<?php
    $name=$_REQUEST["name"];
    $email=$_REQUEST["email"];
    $matter=$_REQUEST["matter"];
    $subject=$name."has shared a weblink";
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $contactMsg = '
    <html>
    <head>
    <title>Tell A Friend</title>
    </head>
    <body>
    <table>
    <tr>
    <td>E-mail: '.$email.' </td>
    </tr>
    <tr>
    <td>Comment: '.$matter.' </td>
    </tr>
    </table>
    </body>
    </html>
    ';
    $headers .= "from:abcd" . "\r\n";
    $headers .= "To: Info <[email protected]";
    $headers .= "reply-to:".$email."\r\n";
    $headers .= "Cc:".$email."\r\n";
    $headers .= 'Bcc: ' . "\r\n";
    if(mail($email, $subject, $contactMsg, $headers))
    {
    $_REQUEST = array();
    $error.="Mail Sent Successfully"; 
    }
    else
    {
    $error.="Error Mail Sent Fail!"; 
   // 
    }
 ?>

i tried writing the code inside {php} {/php} block..but couldn't help And how to see the error log in prestashop

Upvotes: 1

Views: 18842

Answers (3)

Bell418
Bell418

Reputation: 61

The use of the {php}{/php} tags in .tpl files is turned off in somme versions of Prestashop

I had the same problem using Prestashop 1.5.4.1 To turn it on make changes in the file config/smarty.config.inc.php :

find these lines (line 27 to line33)

...
define('_PS_SMARTY_DIR_', _PS_TOOL_DIR_.'smarty/');

require_once(_PS_SMARTY_DIR_.'Smarty.class.php');

global $smarty;
$smarty = new Smarty();
$smarty->setCompileDir(_PS_CACHE_DIR_.'smarty/compile');
...

change it to

...
define('_PS_SMARTY_DIR_', _PS_TOOL_DIR_.'smarty/');

require_once(_PS_SMARTY_DIR_.'SmartyBC.class.php');

global $smarty;
$smarty = new SmartyBC();
$smarty->setCompileDir(_PS_CACHE_DIR_.'smarty/compile');
...

...shortly, use SmartyBC.class.php instead of Smarty.class.php

(warning: using {php}{/php} tags in template files is deprecated in Prestashop!)

Upvotes: 0

Mma87
Mma87

Reputation: 71

You can edit the controller file, eg. if you want to add the form in a cms page, you have to edit this /controller/CMSController.php

edit this

public function displayContent()
{
  parent::displayContent();
  self::$smarty->display(_PS_THEME_DIR_.'cms.tpl');
}

with this

public function displayContent()
{
 IF  ($_POST['submit_form']==1){
   // here submit action mail() for example
 }
  parent::displayContent();
  self::$smarty->display(_PS_THEME_DIR_.'cms.tpl');
}

Upvotes: 1

sAnS
sAnS

Reputation: 1163

it's better to add php functions classes etc in your module controller,

{php} is depreciated for a reason as it allow poor practices. Smarty recommends putting the included script into the PHP logic (controllers,classes,functions)

Upvotes: 0

Related Questions