TASIOMIND.DEV — OPERATIONAL▸▸▸FULL STACK DEVELOPER @ GWQ SERVICEPLUS AG▸▸▸FOUNDER — K8SGPT.AI▸▸▸OPEN SOURCE: ACTIVE▸▸▸DISTRIBUTED SYSTEMS / KUBERNETES / AI▸▸▸RUST + GO + PYTHON▸▸▸FIELD TESTED / STATUS — NOMINAL▸▸▸LOCATION: EUROPE/BERLIN▸▸▸TASIOMIND.DEV — OPERATIONAL▸▸▸FULL STACK DEVELOPER @ GWQ SERVICEPLUS AG▸▸▸FOUNDER — K8SGPT.AI▸▸▸OPEN SOURCE: ACTIVE▸▸▸DISTRIBUTED SYSTEMS / KUBERNETES / AI▸▸▸RUST + GO + PYTHON▸▸▸FIELD TESTED / STATUS — NOMINAL▸▸▸LOCATION: EUROPE/BERLIN▸▸▸

Misc. OpenCart Snippets

October 26, 2017

Controller

See if a user is logged in

code
$this->customer->isLogged() // returns true/false

Load a language in a controller.php file

code
$this->load->language('account/register');

Set language variables (in controller.php) to be used to template.tpl files

code
$data['heading_title'] = $this->language->get('heading_title');

Reference URLs

code
$this->url->link('account/logout') // URL for logout

// URL saved as a variable which will be used in the template. e.g. <form action="<?php $action; ?>">
$data['action'] = $this->url->link('account/register', '', true);

Get a config value

code
$this->config->get('config_account_id')

Load a model

code
$this->load->model('catalog/information');

Load other controllers

code
// LOAD OTHER CONTROLLERS
$data['column_left'] = $this->load->controller('common/column_left');
$data['column_right'] = $this->load->controller('common/column_right');
$data['content_top'] = $this->load->controller('common/content_top');
$data['content_bottom'] = $this->load->controller('common/content_bottom');
$data['footer'] = $this->load->controller('common/footer');
$data['header'] = $this->load->controller('common/header');

Load a template and send it data

code
public function index() {
  // Code goes here

  $this->response->setOutput($this->load->view('account/register', $data));
}

Sending responses from controller.php with $this->response

code
$this->response->redirect($this->url->link('account/success'));
$this->response->setOutput($this->load->view('account/register', $data));
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));

Sending back JSON data

code
// Define an array
$json = array();

// add data to that array
foreach ($custom_fields as $custom_field) {
  $json[] = array(
    'custom_field_id' => $custom_field['custom_field_id'],
    'required'        => $custom_field['required']
    );
  }

// send the array as a response
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));

Breadcrumbs

code
// BREADCRUMBS
$data['breadcrumbs'] = array();

$data['breadcrumbs'][] = array(
	'text' => $this->language->get('text_home'),
	'href' => $this->url->link('common/home')
);

$data['breadcrumbs'][] = array(
	'text' => $this->language->get('text_account'),
	'href' => $this->url->link('account/account', '', true)
);

$data['breadcrumbs'][] = array(
	'text' => $this->language->get('text_register'),
	'href' => $this->url->link('account/register', '', true)
);