Jump to content

Integrate Header / Footer in external PHP file


Recommended Posts

Hello,

 

Trying to wrap a custom external PHP file with the header / footer in use by the prestashop theme.

 

Current structure is as follows:

 

/index.php ---- The file I wish to wrap the header / footer code in

/shop/ * ---- the path where prestashop is currently installed.

 

I found the following code on the internet, which looks correct given my experience of a similar process using wordpress header / footer to wrap around code, but the problem is it ALWAYS redirects to the shop page --- I need to be able to include the header / footer WITHOUT redirecting.

 

<?php
global $smarty;
global $cookie;
include(dirname(__FILE__).'/shop/config/config.inc.php');
include(dirname(__FILE__).'/shop/header.php');
// User content here
include(dirname(__FILE__).'/shop/footer.php');
?>

 

What am I missing in order to wrap the header and footer inside /index.php without the script redirecting to the shop ?

Edited by hq6u5doz62 (see edit history)
Link to comment
Share on other sites

I believe you shouldn't do it. Prestashop uses smarty as templating system and generates parts of the page using controller. You may be able to achieve the result but it's absolutely not the right way to implement a new page in my opinion. You should drop those files inside tpls and create your own controller :)

Link to comment
Share on other sites

For this project, we must have this code outside as the CMS is much more advanced than the one built into PrestaShop, and it would be an unreasonably large job to build such a system into a shopping cart application, not to mention a whole tonne of core changes would need to be done to PrestaShop in order to maximize on SEO.

 

Respectfully, I wasn't asking for opinions, I was asking for a solution to how this can be achieved. Clearly something is missing, but I know the above is (from what I understand), the correct way to do this.

 

I would prefer not to reverse trace to the code which is forcing the redirect -- rather to simply bypass it with minimal code. In wordpress, it is done with a simple DEFINE tag to control the drawing of the template.

Link to comment
Share on other sites

Hi hq6u5doz62,

 

 

If the redirect is your main concern, the FAQ of this WordPress plugin may be of use to you:

http://wordpress.org/plugins/prestashop-integration/faq/

 

It includes a code snippet to override the default behavior in classes/shop/Shop.php. The code there is specific to the plugin, but it should give you a good start.

 

Hope this helps,

 

Brit

Link to comment
Share on other sites

@britalb

 

Perfect!!!!! Final result is as follows (sharing for those who also need this)

 

/index.php

<?php
global $smarty;
global $cookie;
define('PRESTASHOP_INTEGRATION_VERSION', true);
include(dirname(__FILE__).'/shop/config/config.inc.php');
include(dirname(__FILE__).'/shop/header.php');
// Website content here
include(dirname(__FILE__).'/shop/footer.php');
?>

 

 

/shop/classes/shop/Shop.php

 

1. Add


			if (defined('PRESTASHOP_INTEGRATION_VERSION')) {
				$shop = $default_shop;
			} else {

 

Just below ( around line 367 )

	   	 if (!Validate::isLoadedObject($default_shop))
				throw new PrestaShopException('Shop not found');

 

 

2. Add

}

 

Just below

	   	 header('location: '.$url);
			exit;

 

 

The final block of code should look something like the following

	   	 // Hmm there is something really bad in your Prestashop !
			if (!Validate::isLoadedObject($default_shop))
				throw new PrestaShopException('Shop not found');

			if (defined('PRESTASHOP_INTEGRATION_VERSION')) {
				$shop = $default_shop;
			} else {

			$params = $_GET;
			unset($params['id_shop']);
			if (!Configuration::get('PS_REWRITING_SETTINGS'))
				$url = 'http://'.$default_shop->domain.$default_shop->getBaseURI().'index.php?'.http_build_query($params);
			else
			{
				// Catch url with subdomain "www"
				if (strpos($default_shop->domain, 'www.') === 0 && 'www.'.$_SERVER['HTTP_HOST'] === $default_shop->domain
					|| $_SERVER['HTTP_HOST'] === 'www.'.$default_shop->domain)
					$uri = $default_shop->domain.$_SERVER['REQUEST_URI'];
				else
					$uri = $default_shop->domain.$default_shop->getBaseURI();

				if (count($params))
					$url = 'http://'.$uri.'?'.http_build_query($params);
				else
					$url = 'http://'.$uri;
			}
			header('location: '.$url);
			exit;
			}

 

As a recommendation to PrestaShop developers: Please integrate this flag into the core of next release. There is no side effects aside from when using this flag to be able to draw content outside of prestashop. It does not interfere in the way the cart works as is.

Edited by hq6u5doz62 (see edit history)
Link to comment
Share on other sites

Glad to help! Thanks for sharing your code.

 

You may want to rename your constant from PRESTASHOP_INTEGRATION_VERSION to something like EXTERNAL_PAGE, particularly as that is already the name of a plugin.

Link to comment
Share on other sites

I understand why you may suggest that, however consider this --- the code will not cancel the redirect of PrestaShop, unless it's defined with any value --- one could modify the Shop.php, for multiple defines, however since it is unlikely that one would be using both wordpress + plugin, and an external custom script launching the wrapper at the same time, it is reasonable to assume that a single define which acts as a flag (regardless of name), should suffice on a global scale.

 

Similar to the flag you need to define to draw a wordpress header and footer, to prevent it from painting immediately.

Link to comment
Share on other sites

Fair enough. I just personally prefer my constant names to be as clear and descriptive as possible. "PRESTASHOP_INTEGRATION_VERSION" sounds like what it is - the current revision of a module or plugin.

Link to comment
Share on other sites

@britalb

 

I agree. It does sound wacky -- would be better if it was named something like

 

PS_REDIRECT

 

With a check more specific beyond the existance of the define, such as

 

if(defined('PS_REDIRECT')) {

if(cbool(PS_REDIRECT) == false) {

// cancel redirect

$shop == $default_shop;

exit;

}

}

 

					 // Hmm there is something really bad in your Prestashop !
						    if (!Validate::isLoadedObject($default_shop))
								    throw new PrestaShopException('Shop not found');

						    if (defined('PS_REDIRECT')) {
								    if(cbool(PS_REDIRECT) == false) {
										    $shop = $default_shop;
										    exit;
								    }
								    goto ps_redirect_active;
						    } else {
ps_redirect_active:
						    $params = $_GET;
						    unset($params['id_shop']);
						    if (!Configuration::get('PS_REWRITING_SETTINGS'))
								    $url = 'http://'.$default_shop->domain.$default_shop->getBaseURI().'index.php?'.http_build_query($params);
						    else
						    {
								    // Catch url with subdomain "www"
								    if (strpos($default_shop->domain, 'www.') === 0 && 'www.'.$_SERVER['HTTP_HOST'] === $default_shop->domain
										    || $_SERVER['HTTP_HOST'] === 'www.'.$default_shop->domain)
										    $uri = $default_shop->domain.$_SERVER['REQUEST_URI'];
								    else
										    $uri = $default_shop->domain.$default_shop->getBaseURI();
								    if (count($params))
										    $url = 'http://'.$uri.'?'.http_build_query($params);
								    else
										    $url = 'http://'.$uri;
						    }
						    header('location: '.$url);
						    exit;
						    }

 

I know ... lazy code using goto with placeholders, but it works, and it's less repetetive.

Link to comment
Share on other sites

  • 6 months later...
  • 4 years later...

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