Jump to content
mdusamaansari

How to use PHP code in smarty product page?

Recommended Posts

Hi Mates,

 

I have a PHP code which is working with PS 1.2.4.0.

 

Its a countdown timer which provides difference between current time and 15th hour that day. Its a process which works daily.

 

Herewith providing the example link.

 

http://www.old.unikmobil.dk/iphone-5-biltilbehor/2411-usb-billader-12v-24v-sort.html

 

Here is my code:

{php}
$todayDate = date("j");
$todayMonth = date("n");
$todayTime = date("G");
$todayYear = date("Y");
$leapYear = $todayYear/4;
$leapYear = strpos($leapYear, ".");

if(empty($leapYear)) {
}

if($todayTime < $this->_tpl_vars[special][PS_DELIVERY_COUNTDOWN_TIME]) {
	$date = $todayDate;
	$month = $todayMonth;
}
else {
	if($todayMonth == 1) {
		if($todayDate == 31){
			$date = 1;
			$month = 2;
		}
		else {
			$date = $todayDate+1;
			$month = $todayMonth;
		}
	}
	elseif($todayMonth == 2) {
		if((empty($leapYear)) && ($todayDate == 29)) {
			$date = 1;
			$month = 3;
		}
		elseif(($todayDate == 28) && !empty($leapYear)){
			$date = 1;
			$month = 3;
		}
		else {
			$date = $todayDate+1;
			$month = $todayMonth;
		}
	}
	elseif($todayMonth == 3) {
		if($todayDate == 31){
			$date = 1;
			$month = 4;
		}
		else {
			$date = $todayDate+1;
			$month = $todayMonth;
		}
	}
	elseif($todayMonth == 4) {
		if($todayDate == 30){
			$date = 1;
			$month = 5;
		}
		else {
			$date = $todayDate+1;
			$month = $todayMonth;
		}
	}
	elseif($todayMonth == 5) {
		if($todayDate == 31){
			$date = 1;
			$month = 6;
		}
		else {
			$date = $todayDate+1;
			$month = $todayMonth;
		}
	}
	elseif($todayMonth == 6) {
		if($todayDate == 30){
			$date = 1;
			$month = 7;
		}
		else {
			$date = $todayDate+1;
			$month = $todayMonth;
		}
	}
	elseif($todayMonth == 7) {
		if($todayDate == 31){
			$date = 1;
			$month = 8;
		}
		else {
			$date = $todayDate+1;
			$month = $todayMonth;
		}
	}
	elseif($todayMonth == 8) {
		if($todayDate == 31){
			$date = 1;
			$month = 9;
		}
		else {
			$date = $todayDate+1;
			$month = $todayMonth;
		}
	}
	elseif($todayMonth == 9) {
		if($todayDate == 30){
			$date = 1;
			$month = 10;
		}
		else {
			$date = $todayDate+1;
			$month = $todayMonth;
		}
	}
	elseif($todayMonth == 10) {
		if($todayDate == 31){
			$date = 1;
			$month = 11;
		}
		else {
			$date = $todayDate+1;
			$month = $todayMonth;
		}
	}
	elseif($todayMonth == 11) {
		if($todayDate == 30){
			$date = 1;
			$month = 12;
		}
		else {
			$date = $todayDate+1;
			$month = $todayMonth;
		}
	}
	elseif($todayMonth == 12) {
		if($todayDate == 31){
			$date = 1;
			$month = 1;
			$todayYear = $todayYear+1;
		}
		else {
			$date = $todayDate+1;
			$month = $todayMonth;
		}
	}
}

$target =  mktime($this->_tpl_vars[special][PS_DELIVERY_COUNTDOWN_TIME], 0, 0, $month, $date, $todayYear);

$curTime = time();
$nextReset = $target-$curTime;

if((date("l") == 'Friday' AND $nextReset > 43200) OR (date("l") == 'Saturday') OR (date("l") == 'Sunday')) {
	$this->assign('mandag','True');
}
else {
	if($nextReset <= 43200) {
		$sendDay = "I DAG";	
	}
	else {
		$sendDay = "I MORGEN";
	}
}

{/php}
{if $product->quantity > 0}
{literal}
<script type="text/javascript">
var nTime = {/literal}{php} echo $nextReset; {/php}{literal};

function countDown() {
    var nH = Math.floor(nTime/3600),
    nRest = nTime%3600,
    nM = Math.floor(nRest/60),
    nS = nRest%60;
    if (nM<10) nM = "0" + nM;
    if (nS<10) nS = "0" + nS;
    oTime.firstChild.nodeValue = (nH+" time(r) "+nM+" min "+nS+" sek");
    if (nTime==0) {
        window.location.href=window.location.href
    } else {
        nTime--;
        setTimeout("countDown()", 1000);
    }
}
var oTime = null;
window.onload = function() {
    oTime = document.getElementById("timeDisplay");
    countDown();
}
</script>

{/literal}
{/if}
{if $product->quantity > 0}
{if $special.PS_DELIVERY_COUNTDOWN == '1'}
	{if $special.PS_DELIVERY_COUNTDOWN_MSG != ""}
		<div class="timeleft">{$special.PS_DELIVERY_COUNTDOWN_MSG}</div>
	{elseif $mandag == 'True'}
		<div class="timeleft">Hvis du bestiller nu,<br />
		sender vi pakken <strong>MANDAG</strong> !
		</div>
	{else}
		<div class="timeleft">Hvis du bestiller inden<br />
		<span id="timeDisplay"> </span><br />
		sender vi pakken <strong>{php} echo $sendDay; {/php}</strong> !
		</div>
	{/if}
{/if}
{/if}

I need this code to implemented in PS 1.5.6.1.

 

Please help me in this.

 

Thanks in advance.

Share this post


Link to post
Share on other sites

{php}{/php} tags no longer works in prestashop 1.5.6.1

these tags are disabled.

 

if you want to use it, you have to turn it on again.

check this 

 

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

Share this post


Link to post
Share on other sites

Hello Usman,

 

Follow these steps to add timer in product page,

 

1) Create countdown.js file in your themes js folder, and put this code into that file

(function($) {
	$.fn.countdown = function(options, callback) {

		//custom 'this' selector
		thisEl = $(this);

		//array of custom settings
		var settings = { 
			'date': null,
			'format': null,
			'curr_date': null
		};

		//append the settings array to options
		if(options) {
			$.extend(settings, options);
		}
		
		//main countdown function
		function countdown_proc() {
			
			eventDate = Date.parse(settings['date']) / 1000;
			currentDate = Math.floor($.now() / 1000);
			if(eventDate <= currentDate) {
				callback.call(this);
				clearInterval(interval);
			}
			
			seconds = eventDate - currentDate;
			
			days = Math.floor(seconds / (60 * 60 * 24)); //calculate the number of days
			seconds -= days * 60 * 60 * 24; //update the seconds variable with no. of days removed
			
			hours = Math.floor(seconds / (60 * 60));
			seconds -= hours * 60 * 60; //update the seconds variable with no. of hours removed
			
			minutes = Math.floor(seconds / 60);
			seconds -= minutes * 60; //update the seconds variable with no. of minutes removed
			
			//conditional Ss
			if (days == 1) { thisEl.find(".timeRefDays").text("day"); } else { thisEl.find(".timeRefDays").text("days"); }
			if (hours == 1) { thisEl.find(".timeRefHours").text("hr"); } else { thisEl.find(".timeRefHours").text("hrs"); }
			if (minutes == 1) { thisEl.find(".timeRefMinutes").text("min"); } else { thisEl.find(".timeRefMinutes").text("mins"); }
			if (seconds == 1) { thisEl.find(".timeRefSeconds").text("sec"); } else { thisEl.find(".timeRefSeconds").text("secs"); }
			
			//logic for the two_digits ON setting
			if(settings['format'] == "on") {
				days = (String(days).length >= 2) ? days : "0" + days;
				hours = (String(hours).length >= 2) ? hours : "0" + hours;
				minutes = (String(minutes).length >= 2) ? minutes : "0" + minutes;
				seconds = (String(seconds).length >= 2) ? seconds : "0" + seconds;
			}
			
			//update the countdown's html values.
			if(!isNaN(eventDate)) {
				thisEl.find(".days").text(days);
				thisEl.find(".hours").text(hours);
				thisEl.find(".minutes").text(minutes);
				thisEl.find(".seconds").text(seconds);
			} else { 
				alert("Invalid date. Here's an example: 12 Tuesday 2012 17:30:00");
				clearInterval(interval); 
			}
		}
		
		//run the function
		countdown_proc();
		
		//loop the function
		interval = setInterval(countdown_proc, 1000);
		
	}
}) (jQuery);

2) Update ProductController.php from controllers/front folder with attached one.

3) open product.tpl file from you theme folder,

a) Add this code

<script type="text/javascript">
$(document).ready(function(){
	$("#countdown").countdown({
		date: '{$next_date_time}',
		format: "on"
	},
	function() {
		// callback function
	});
	
});
</script>

right after this code 

{include file="$tpl_dir./errors.tpl"}

b )Add this code 

{* Timer Starts *}
	<div id="timer">
		<ul id="countdown">
				<li>
					<span class="days">00</span>
					<p class="timeRefDays">days</p>
				</li>
				<li>
					<span class="hours">00</span>
					<p class="timeRefHours">hrs</p>
				</li>
				<li>
					<span class="minutes">00</span>
					<p class="timeRefMinutes">mins</p>
				</li>
				<li>
					<span class="seconds">00</span>
					<p class="timeRefSeconds">secs</p>
				</li>
			</ul>
	</div>
	{* Timer Ends *}

before this line

<!-- availability -->

4) Adding some style, Open global.css from yourthemes folder,

add this code at the end,

/* ---------- TIMER ---------- */
ul#countdown li {
  display: inline-block;
  background:#333333;
  width: 44px;
  text-align: center;
  font-family: georgia;margin-right: 3px;
  color: #FFFFFF; -moz-border-radius: 5px; /* FF1+ */ -webkit-border-radius: 5px; /* Saf3+, Chrome */border-radius: 5px; /* Opera 10.5, IE 9 */
}
ul#countdown li span {
  font-size: 24px;
  font-weight: bold;
  color: #FFFFFF;
  height: 108px;
  position: relative;
}
ul#countdown li span::before {
  content: '';
  width: 100%;
  height: 1px;
  position: absolute;
}
ul#countdown li p.timeRefDays,
ul#countdown li p.timeRefHours,
ul#countdown li p.timeRefMinutes,
ul#countdown li p.timeRefSeconds {
margin: 2px 0 6px;
font-size: 16px;
padding: 0;
color: white;
}

Do changes as above and Enjoy!!!

 

Please Note that, these all changes i have done is with default prestashop theme, one can use these as their own way.

ProductController.php

Edited by divyeshp

Share this post


Link to post
Share on other sites

Thank you  divyeshp i tried your code but the format of date for ProductController.php this are examples      

 

$today = date('M d,Y H:i:s');
$next_date_time = date('M d,Y H:i:s', strtotime($today)+54000); // 15hrs=60*60*15= 54000

 

Cheers,

Lidaniel

Share this post


Link to post
Share on other sites

Thank you  divyeshp i tried your code but the format of date for ProductController.php this are examples      

 

$today = date('M d,Y H:i:s');

$next_date_time = date('M d,Y H:i:s', strtotime($today)+54000); // 15hrs=60*60*15= 54000

 

Cheers,

Lidaniel

Hello Lidaniel,

Format of date should be 'd F Y H:i:s'

Share this post


Link to post
Share on other sites

Hi divyeshp May Iask, How can I get the server time using your javascript? coundown.js

Because your script get the browser time.

 

Thanks,

Lidaniel

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More