Jump to content

How to use external PHP code inside Prestashop?


mdusamaansari

Recommended Posts

 

 

I have a file which do some process, I need to include my external PHP file into prestashop and get those values dispalyed on top of all the pages using smarty.

require this file in FrontController.php file (classes/controller/frontController.php)

then in init() function add code to pass variables to smarty array

 

for example:

$this->context->smarty->assign('myVariable', $some_variable_from_your_php_library);

then you will be able to use {$myVariable} anywhere you want in your template files, in this case - also in header.tpl file :)

Link to comment
Share on other sites

require this file in FrontController.php file (classes/controller/frontController.php)

then in init() function add code to pass variables to smarty array

 

for example:

$this->context->smarty->assign('myVariable', $some_variable_from_your_php_library);

then you will be able to use {$myVariable} anywhere you want in your template files, in this case - also in header.tpl file :)

Hi Vekia,

 

I am getting error like this "Trying to get property of non-object" and "Notice: Undefined index:"

 

The value is not getting displayed.

 

Thanks in advance.

Link to comment
Share on other sites

can you show your code where you define this variable, and also how the code from external library (variable that you're trying to use) looks like?

Hi Vekia,

 

Here is my external PHP file.

<?php
	$selectd = "SELECT * FROM `ps_specific_price` WHERE `id_product` IN(".implode(',',$cart_product_id).')';
	$squeryd = mysql_query($selectd);
	$reduction_price = array();
	$product_id = array();
	while($rowd = mysql_fetch_array($squeryd))
        	{
				$product_id[] = $rowd['id_product'];
				$reduction_price[] = $rowd['reduction'];
				$selectf = "SELECT * FROM ps_cart_product WHERE id_product IN(".implode(',',$product_id).')';
				$squeryf = mysql_query($selectf) or die(mysql_error());
				$resultf = mysql_fetch_array($squeryf) or die(mysql_error());
					$quantity = $resultf['quantity'];
					
					foreach($reduction_price as $price){
						$total_price = $price * $quantity;
					}
				$usama_total_amount = $usama_total_amount + $total_price;
			}
	// This is the variable I need to display.
	$usama_total_amount;
?>

Here is my FrontController.php which is in (classes/controller/).

	public function init()
	{
		/*
		 * Globals are DEPRECATED as of version 1.5.
		 * Use the Context to access objects instead.
		 * Example: $this->context->cart
		 */
		global $useSSL, $cookie, $smarty, $cart, $iso, $defaultCountry, $protocol_link, $protocol_content, $link, $css_files, $js_files, $currency;

		if (self::$initialized)
			return;
		self::$initialized = true;

		parent::init();

//My code starts here
		require("http://".$protocol_content.Tools::getHttpHost().__PS_BASE_URI__."customersaved/csaved.php");
		
		$this->context->smarty->assign('usama_total_amount', $usama_total_amount);
//My code ends here
		
		// If current URL use SSL, set it true (used a lot for module redirect)
		if (Tools::usingSecureMode())
			$useSSL = true;

		// For compatibility with globals, DEPRECATED as of version 1.5
		$css_files = $this->css_files;
		$js_files = $this->js_files;

		// If we call a SSL controller without SSL or a non SSL controller with SSL, we redirect with the right protocol
		if (Configuration::get('PS_SSL_ENABLED') && ($_SERVER['REQUEST_METHOD'] != 'POST') && $this->ssl != Tools::usingSecureMode())
		{	
			header('HTTP/1.1 301 Moved Permanently');
			header('Cache-Control: no-cache');
			if ($this->ssl)					
				header('Location: '.Tools::getShopDomainSsl(true).$_SERVER['REQUEST_URI']);
			else						
				header('Location: '.Tools::getShopDomain(true).$_SERVER['REQUEST_URI']);
			exit();
		}

Here is my header.tpl file which is (themes/default/)

			<!-- Header -->
			<div id="header" class="grid_9 alpha omega">
				<a id="header_logo" href="{$base_dir}" title="{$shop_name|escape:'htmlall':'UTF-8'}">
<!-- My code starts here -->
                {$usama_total_amount}
<!-- My code ends here -->
					<!--<img class="logo" src="{$logo_url}" alt="{$shop_name|escape:'htmlall':'UTF-8'}" {if $logo_image_width}width="{$logo_image_width}"{/if} {if $logo_image_height}height="{$logo_image_height}" {/if}/>-->
				</a>
				<div id="header_right" class="grid_9 omega">
					{$HOOK_TOP}
				</div>
			</div>

Thanks in advance.

Link to comment
Share on other sites

require(.$protocol_content.Tools::getHttpHost().__PS_BASE_URI__."customersaved/csaved.php");

 

it will not work

it's not possible to require external php files in that way (with http)

you will not include php file contents, but only results of it.

the same results as you see when you open this file in browser.

Link to comment
Share on other sites

this file is on the same hosting account / domain ?

 

Vekia,

 

Successfull, very much thanks for your support.

 

I have wrapped my external codes inside a function and returned a values which is need, and I called my external function by including external php file.

 

Now I am getting value which is in my external function and getting displayed in my header.tpl.

 

But I have used php core functions like, mysql_connect, include in my external code, I am getting those errors in my smarty.

 

How to overcome this.

Link to comment
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
×
×
  • Create New...