Post by misspiggy » Wed May 16, 2018 2:12 am

Hi everyone,

I want to get a specific information page(s) so i can get it displayed where i want instead of having all the infrmation pages.
See below example, based on opencart 1.54
viewtopic.php?t=30529
However as of opencart 3.0 you are not allowed to use php in the twig file. So i have a little bit trouble adjusting the example for opencart 1.54 to oc 3.0

I have been doing some coding. And although i get something i am not there yet
So i have added this piece of code as a test in the controller/common/footer.php

Code: Select all

if ( $informations['information_id'] = '3' ) {
				$data['privacy'][] = array(
					'title' => $result['title'],
					'information_id' => $result['information_id'],
					'href'  => $this->url->link('information/information', 'information_id=' . $result['information_id'])
				);
			}
		if ( $informations['information_id'] = '5' ) {
				$data['term'][] = array(
					'title' => $result['title'],
					'information_id' => $result['information_id'],
					'href'  => $this->url->link('information/information', 'information_id=' . $result['information_id'])
				);
			}
behind

Code: Select all

	foreach ($this->model_catalog_information->getInformations() as $result) {
			if ($result['bottom']) {
				$data['informations'][] = array(
					'title' => $result['title'],
				'information_id' => $result['information_id'],
					'href'  => $this->url->link('information/information', 'information_id=' . $result['information_id'])
				);
			}
		}
and then on the common/footer.twig
I have added

Code: Select all

  {% for privacys in privacy %}
               <a href="{{ privacys.href }}">{{ privacys.title }}yo</a>
           {% endfor %}
                {% for terms in term %}
               <a href="{{ terms.href }}">{{ terms.title }}tse</a>
               {% endfor %}
               
So i only get the last link double. I think i am calling the code in the twig file wrong. I don't think i should use the for function.
Any help is very much appreciated.

Thanks

Newbie

Posts

Joined
Fri Nov 18, 2011 7:37 pm

Post by sw!tch » Wed May 16, 2018 2:59 am

Really there are better ways to do this, but you can backup and try this.

You can undo your controller changes and then try replacing the entire foreach loop (eg ($this->model_catalog_information->getInformations() as $result) {..... } with the below. Your twig changes should be fine.

Find in catalog/controller/common/footer.php

Code: Select all

foreach ($this->model_catalog_information->getInformations() as $result) {
			if ($result['bottom']) {
				$data['informations'][] = array(
					'title' => $result['title'],
					'href'  => $this->url->link('information/information', 'information_id=' . $result['information_id'])
				);
			}
		}
Replace with

Code: Select all

$data['privacy'] = array();
$data['term'] = array();

foreach ($this->model_catalog_information->getInformations() as $result) {
		if ( $result['information_id'] == '3' ) {
				$data['privacy'][] = array(
					'title' => $result['title'],
					'information_id' => $result['information_id'],
					'href'  => $this->url->link('information/information', 'information_id=' . $result['information_id'])
				);
			}
			
		if ( $result['information_id'] == '5' ) {
				$data['term'][] = array(
					'title' => $result['title'],
					'information_id' => $result['information_id'],
					'href'  => $this->url->link('information/information', 'information_id=' . $result['information_id'])
				);
			}

		if ($result['bottom']) {
				$data['informations'][] = array(
					'title' => $result['title'],
					'href'  => $this->url->link('information/information', 'information_id=' . $result['information_id'])
				);
			}
		}
Untested try at your own risk - Clear your template caches.

Full Stack Web Developer :: Send a PM for Custom Work.
Backup and learn how to recover before you make any changes!


Active Member

Posts

Joined
Sat Apr 28, 2012 2:32 pm

Post by misspiggy » Wed May 16, 2018 12:16 pm

Thank you very much. Your solution did the trick!

Really there are better ways to do this, but you can backup and try this.
I had the feeling already that my method is not the most efficient, but i was happy i got something after struggling with the code.

Any suggestions how to make it better?

Newbie

Posts

Joined
Fri Nov 18, 2011 7:37 pm

Post by OSWorX » Wed May 16, 2018 3:17 pm

misspiggy wrote:
Wed May 16, 2018 12:16 pm
Thank you very much. Your solution did the trick!

Really there are better ways to do this, but you can backup and try this.
I had the feeling already that my method is not the most efficient, but i was happy i got something after struggling with the code.

Any suggestions how to make it better?
Instead of using a foreach loop, use switch.
Why?
Switch is faster.

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Administrator
Online

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by paulfeakins » Wed May 16, 2018 4:47 pm

OSWorX wrote:
Wed May 16, 2018 3:17 pm
Instead of using a foreach loop, use switch.
Why?
Switch is faster.
I bet the difference isn't even measurable.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by sw!tch » Wed May 16, 2018 5:07 pm

misspiggy wrote:
Wed May 16, 2018 12:16 pm
Thank you very much. Your solution did the trick!

Really there are better ways to do this, but you can backup and try this.
I had the feeling already that my method is not the most efficient, but i was happy i got something after struggling with the code.

Any suggestions how to make it better?
You could also refactor the array a bit so you can lose the for loop in the twig template, less code. What you have works, if you start adding like 20 custom links then I would look into better refactoring it, or another solution.

Good luck.

Full Stack Web Developer :: Send a PM for Custom Work.
Backup and learn how to recover before you make any changes!


Active Member

Posts

Joined
Sat Apr 28, 2012 2:32 pm
Who is online

Users browsing this forum: Amazon [Bot] and 118 guests