Post by supremeSEO » Tue Sep 10, 2019 4:07 am

I have a main website with OC in a subdirectory. (site.com/oc). The main site is run on basic static php pages. I need to do a php include of the OC menu and footer on the main website so that:
1. Navigation and footer is consistent across main and OC segments
2. When I change navigation/footer on OC side the main website navigation/footer also changes to reflect the updated nav.

Q: How to do a php include of the OC nav and footer on non-OC main website in primary directory.

Thanks in advance.

Newbie

Posts

Joined
Fri Mar 18, 2016 1:09 am

Post by thekrotek » Tue Sep 10, 2019 4:33 am

And you really expect someone to explain you everything you need to do to include OC MVC in your main "static" store? Well, good luck.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by supremeSEO » Tue Sep 10, 2019 4:52 am

thekrotek: All respect. With so many using OC in a subdirectory this request should be a given in terms of having a resource somewhere to explain the process. Am I the only one thinking about the user experience? I think not. Maybe I am wrong.

I "EXPECT" nothing. I request help. Comments like "You really expect help?" Are not helpful at all. If you know please help. If you do not know please save everybody time and not make empty comments. If you know, but it is too much for you to reach out with "real" helpful advice but you are willing to do it as a project then just say so and make some money in the process.

I understand how to do a basic php include. For example make a menu save it as menu.php and then include it on another page.

The problem I am running into is how to get the variables to show correctly. For example {{menu}} to actually show the OC menu.

For all those willing to provide real help many thanks in advance.

Newbie

Posts

Joined
Fri Mar 18, 2016 1:09 am

Post by thekrotek » Tue Sep 10, 2019 5:02 am

You indeed have no idea what you're asking about and what MVC is...

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by supremeSEO » Tue Sep 10, 2019 5:17 am

thelrotek: My question is clear. My knowledge may be weak. Not as weak as your responses though. Please refrain from commenting if you have no value to add to the post. I am looking for help, suggestions, resources, etc. Maybe in what you feel is your infinite wisdom there are truly valuable insights you could pass on. Maybe I am wrong about the insights and maybe you are wrong about your wisdom? Everyone has to start somewhere and no one really needs someone mocking them for their lack of knowledge. If you are as wise as you think, maybe adding less post lifting fluff and adding more value to your posts would be a great way to help convince others?

Newbie

Posts

Joined
Fri Mar 18, 2016 1:09 am

Post by letxobnav » Tue Sep 10, 2019 12:43 pm

You could do that but only if you remove any dynamic content from the header and footer first.
So no language, currency, account, shopping cart or whatever is session-dependent can be in there.

Otherwise I would suggest you forget the whole "include idea" and just match the look and feel with css/js only.

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


Active Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by paulfeakins » Tue Sep 10, 2019 6:07 pm

Ok calm down gents :laugh:

Basically: you can't just include some files to achieve what you want because in OpenCart (and many applications) everything goes through index.php

There are lots of different ways you could approach this.

One possible way might be to adjust the OpenCart code so that by passing a variable "header" or "footer" it only displays that part of the page. You could then output that in the right place.

I don't think there's a simple way to do it and if the above doesn't make much sense you should post it as a paid job in Commercial Support.

For quick, professional OpenCart support please email info@antropy.co.uk


User avatar
Expert Member

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - Reigate, Surrey, United Kingdom

Post by letxobnav » Tue Sep 10, 2019 6:26 pm

Not sure where you get the idea we are not calm.

And no, whatever you were thinking about passing variables, forget it, we are talking about displaying the header and footer outside of the opencart implementation.

The only way you can do that is if you cache them and for that they need to be static.

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


Active Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by paulfeakins » Tue Sep 10, 2019 6:29 pm

letxobnav wrote:
Tue Sep 10, 2019 6:26 pm
Not sure where you get the idea we are not calm.
Well that was more for the OP and @thekrotek

letxobnav wrote:
Tue Sep 10, 2019 6:26 pm
And no, whatever you were thinking about passing variables, forget it, we are talking about displaying the header and footer outside of the opencart implementation.

The only way you can do that is if you cache them and for that they need to be static.
Rubbish, we've done it before on a WordPress site with fully dynamic working OC header (including live basket) and footer.

For quick, professional OpenCart support please email info@antropy.co.uk


User avatar
Expert Member

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - Reigate, Surrey, United Kingdom

Post by letxobnav » Tue Sep 10, 2019 9:05 pm

Rubbish, we've done it before
Are you the same Paul who said 2 posts ago....
One possible way might be...
I don't think there's a simple way to do it...
Or are you his more optimistic twin?

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


Active Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by supremeSEO » Tue Sep 10, 2019 9:16 pm

First I want to give Paul a thumbs up. Paul has helped me with many projects and has always done great work. Anyone needing real programming assistance and is intimidated, too busy or not knowledgable enough to handle a task should seriously consider his company's assistance. It's not always cheap but their work is well worth considering if needed. Unfortunately for this site it is a tiny non-profit so cost controls are the biggest issue with outside help. Also, it is a good chance for me to expand my knowledge set in the backend design of OC.

As mentioned I am quite surprised this topic has not been discussed more given how many people use OC in a subdirectory of an alt site.

In regards to this task I know it can be done as Paul has confirmed. As programmers and workers in the field we are most often only limited my our our knowledge and skills.

So as an alternate solution I am thinking about nixing (hiding) top menu categories and then on the static site side coding an include menu and then placing that in the header twig of the OC side. If possible it should yield the near same result except it would be modifying the static include menu file when needed (by Dreamweaver) then using the include static menu on the OC side. Still just a single edit for all menus but a different route. Does this seem viable?

Newbie

Posts

Joined
Fri Mar 18, 2016 1:09 am

Post by paulfeakins » Tue Sep 10, 2019 9:20 pm

letxobnav wrote:
Tue Sep 10, 2019 9:05 pm
Or are you his more optimistic twin?
Haha same guy, it's not simple, but it is possible as we have done it although I didn't code this one personally :)

For quick, professional OpenCart support please email info@antropy.co.uk


User avatar
Expert Member

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - Reigate, Surrey, United Kingdom

Post by paulfeakins » Tue Sep 10, 2019 9:27 pm

supremeSEO wrote:
Tue Sep 10, 2019 9:16 pm
First I want to give Paul a thumbs up. Paul has helped me with many projects and has always done great work. Anyone needing real programming assistance and is intimidated, too busy or not knowledgable enough to handle a task should seriously consider his company's assistance. It's not always cheap but their work is well worth considering if needed.
Thanks very much, always nice to hear :)

supremeSEO wrote:
Tue Sep 10, 2019 9:16 pm
Unfortunately for this site it is a tiny non-profit so cost controls are the biggest issue with outside help. Also, it is a good chance for me to expand my knowledge set in the backend design of OC.
I believe the cart in the OpenCart header is loaded via AJAX after the page loads so you could hardcode most of the header in your PHP application and then use the same AJAX call as OpenCart itself to get the cart items.

For quick, professional OpenCart support please email info@antropy.co.uk


User avatar
Expert Member

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - Reigate, Surrey, United Kingdom

Post by letxobnav » Tue Sep 10, 2019 11:50 pm

Then you can just as well create a page in OC with just the header and footer and use file_get_contents to retrieve it and strip the header and footer html and stick it in your output. But you still have to worry about relative asset links for css/js and your normal links as they will still take you inside oc or make them all absolute.

this code will simply retrieve the url and extract the html content between the <header></header> tags and <footer></footer> tags.

Code: Select all

function content_between_matching_tags($string, $tag) {
	$pattern = "#<\s*?$tag\b[^>]*>(.*?)</$tag\b[^>]*>#s";
	preg_match($pattern, $string, $matches);
	return $matches[1];
}

function get_https_contents ($url,$accept_language='en',$cookie=array()) {
	if (empty($cookie)) $cookie = $_COOKIE;
	$cookie_header = 'cookie: ';
	foreach ($cookie as $name => $value) {
		$cookie_header .= $name.'='.$value.'; ';
	}
	$opts = 
		array('http' => 
				array(
				'protocol_version'	=>	'1.1',
				'method'			=>	"GET",
				'timeout'			=>	60,
				'header'			=>	array("Accept-language: " . $accept_language . "\r\n" . $cookie_header."\r\n" . "connection: close"),
				'user_agent' 		=>	"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; fr; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8"
				)
			);
	$context = stream_context_create($opts);
	$content = file_get_contents($url, false, $context,0);
	if ($content) return $content;
	return false;
}

$content = get_https_contents('https://YOUR_OC_PAGE','en');
error_log(content_between_matching_tags($content,'header'));
error_log(content_between_matching_tags($content,'footer'));

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


Active Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by paulfeakins » Tue Sep 10, 2019 11:58 pm

letxobnav wrote:
Tue Sep 10, 2019 11:50 pm
Then you can just as well create a page in OC with just the header and footer and use file_get_contents to retrieve it and strip the header and footer html and stick it in your output. But you still have to worry about relative asset links for css/js and your normal links as they will still take you inside oc or make them all absolute.
You may need to pass the token though or the server won't know what cart items to send back.

For quick, professional OpenCart support please email info@antropy.co.uk


User avatar
Expert Member

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - Reigate, Surrey, United Kingdom

Post by letxobnav » Wed Sep 11, 2019 12:36 am

you can add this to a php file under your root and call it from your browser and experiment with it.
it will retrieve the page and trip out the header and footer and display them.
It passes the cookies so if your cookies are set right it should be session dynamic.

Code: Select all

<?php
function content_between_different_tags($string, $stag,$etag) {
	$pattern = "#<\s*?$stag\b[^>]*>(.*?)<$etag\b[^>]*>#s";
	preg_match($pattern, $string, $matches);
	return $matches[1];
}
function content_between_matching_tags($string, $tag) {
	$pattern = "#<\s*?$tag\b[^>]*>(.*?)</$tag\b[^>]*>#s";
	preg_match($pattern, $string, $matches);
	return $matches[1];
}
function get_https_contents ($url,$accept_language='en',$cookie=array()) {
	if (empty($cookie)) $cookie = $_COOKIE;
	$cookie_header = 'cookie: ';
	foreach ($cookie as $name => $value) {
		$cookie_header .= $name.'='.$value.'; ';
	}
	$opts = array('http' => array(
							'protocol_version'	=>	'1.1',
							'method'			=>	"GET",
							'timeout'			=>	60,
							'header'			=>	array("Accept-language: " . $accept_language . "\r\n" . $cookie_header."\r\n" . "connection: close"),
							'user_agent' 		=>	"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; fr; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8"
							)
				);
	$context = stream_context_create($opts);
	$content = file_get_contents($url, false, $context,0);
	if ($content) return $content;
	return false;
}
$content = get_https_contents('https://ANY_OF_YOUR_OC_PAGES','en');
echo '<html>';
echo '<head>'.content_between_matching_tags($content,'head').'</head>';
echo '<body>'.content_between_different_tags($content,'body','header');
echo '<header>'.content_between_matching_tags($content,'header').'</header>';
echo '<footer>'.content_between_matching_tags($content,'footer').'</footer>';
echo '</body>';
echo '</html>';
?>

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


Active Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by paulfeakins » Wed Sep 11, 2019 12:42 am

letxobnav wrote:
Wed Sep 11, 2019 12:36 am
you can add this to a php file under your root and call it from your browser and experiment with it.
it will retrieve the page and trip out the header and footer and display them.
When I've done similar things I've used phpQuery:
https://github.com/punkave/phpQuery

For quick, professional OpenCart support please email info@antropy.co.uk


User avatar
Expert Member

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - Reigate, Surrey, United Kingdom

Post by supremeSEO » Wed Sep 11, 2019 7:00 am

Thanks for the great input. This definitely helps get me started.

Newbie

Posts

Joined
Fri Mar 18, 2016 1:09 am

Post by letxobnav » Wed Sep 11, 2019 11:48 am

When I've done similar things I've used phpQuery
Didn't know that one, still, would not put too much effort in it as it is still a "tricky" and fragile solution to a "strange" problem.
I can understand to want to have the same look and feel but to just generate the header and footer and copy them over with associated links....no.

Then again, this way you could add the header and footer of amazon.com to your site as well, until you get a call from their lawyers at least.

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


Active Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by paulfeakins » Wed Sep 11, 2019 4:05 pm

letxobnav wrote:
Wed Sep 11, 2019 11:48 am
Didn't know that one, still, would not put too much effort in it as it is still a "tricky" and fragile solution to a "strange" problem.
I can understand to want to have the same look and feel but to just generate the header and footer and copy them over with associated links....no.
The site we did this on has an amusing name: https://www.getlaidbeds.com/

You can see the WordPress blog but it seems they switched from OpenCart to custom PHP which is why the header doesn't keep the cart contents on the blog any more - but this did work when we built it.

Thinking further about this, it might be better to load the header in with AJAX so that the request comes from the user's browser and should then keep the same session and display the correct cart contents.

For quick, professional OpenCart support please email info@antropy.co.uk


User avatar
Expert Member

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - Reigate, Surrey, United Kingdom
Who is online

Users browsing this forum: No registered users and 16 guests