Getting NULL in template - OpenCart 2.0 - php

I am trying to render product_list.tpl file in home.tpl but it's giving me NULL
Controller File:
/controller/product/product_list.php
Code:
class ControllerProductProductList extends Controller {
public function index() {
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$this->load->model('tool/image');
$filter_data = array(
'filter_tag' => 'featured',
'limit' => 9
);
$data['results'] = $this->model_catalog_product->getProducts($filter_data);
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/productlist.tpl')) {
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/common/productlist.tpl', $data));
} else {
$this->response->setOutput($this->load->view('default/template/common/productlist.tpl', $data));
}
}
}
Template to render
/template/product/productlist.tpl
Code:
<?php var_dump($results); ?>
<h2>Product are here</h2>
Then adding this line in home.php controller
$data['special_mod'] = $this->load->controller('product/product_list');
and printing $special_mod in common/home.tpl file

The problem was in /controller/product/product_list.php
the method $this->response->setOutput doesn't just return the value but send the user to the different page while what I wanted was to just output the productlist.tpl as string so for that I had to replace the code
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/productlist.tpl')) {
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/common/productlist.tpl', $data));
} else {
$this->response->setOutput($this->load->view('default/template/common/productlist.tpl', $data));
}
with
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/productlist.tpl')) {
return $this->load->view($this->config->get('config_template') . '/template/common/productlist.tpl', $data);
} else {
return $this->load->view('default/template/common/productlist.tpl', $data);
}

Related

How to pass multiple objects to a template?

I am trying to understand the MVC method with the use of OOP. However it seems like I've hit the wall here.
I am trying to pass multiple objects to the view. But all I can do so far is pass just one object. The ideal result would be passing multiple objects, while keeping the names that are assigned to them in the controller.
The render, start and end functions in the View class go something like this:
public function render($viewName, $data){
$viewAry = explode('/', $viewName);
$viewString = implode(DS, $viewAry);
if(file_exists(ROOT . DS . 'app' . DS . 'views' . DS . $viewString . '.php')){
include(ROOT . DS . 'app' . DS . 'views' . DS . $viewString . '.php');
include(ROOT . DS . 'app' . DS . 'views' . DS . 'layouts' . DS . $this->_layout . '.php');
}else{
die('The view \"' . $viewName . '\" does not exist.');
}
}
public function content($type){
if($type == 'head'){
return $this->_head;
}elseif ($type == 'body'){
return $this->_body;
}
return false;
}
public function start($type){
$this->_outputBuffer = $type;
ob_start();
}
public function end(){
if($this->_outputBuffer == 'head'){
$this->_head = ob_get_clean();
}elseif($this->_outputBuffer == 'body'){
$this->_body = ob_get_clean();
}else{
die('You must first run the start method.');
}
}
And this is how would the controller look like:
public function indexAction(){
$items = $this->PortalModel->getItems();
$collections = $this->PortalModel->getCollections();
$this->view->render('home/index', $items);
}
So this is how I get the one $data object to the view and loop trough it.
But how could I store multiple results from the database to the view?
You should pass an array of variables into view instead of one variable.
public function indexAction(){
$variables = [
'items' => $this->PortalModel->getItems(),
'collections' => $this->PortalModel->getCollections()
];
$this->view->render('home/index', $variables);
}

Sending POST data from Symfony into Yii controller

I have Yii2 project. And there I also have api written by Symfony.
In Symfony part I have method in the class which send request to Yii controller.
$buzz = $this->container->get('buzz');
$buzz->getClient()->setVerifyHost(false);
$buzz->getClient()->setVerifyPeer(false);
$buzz->getClient()->setTimeOut(false);
$url = $this->container->getParameter('integra_sync_prices');
$sendResult = $buzz->post($url, array('authorization' => $this->container->getParameter('load_token')), array('products' =>
json_encode($productPrices)));
$resultJson = json_decode($sendResult->getContent(), true);
if (isset($resultJson['error']))
throw new \Exception('Site: '.$resultJson['error'], 500);
class IntegraController extends Controller{
public function actionIndex()
{
Yii::$app->response->format = Response::FORMAT_JSON;
$headers = Yii::$app->request->getHeaders();
foreach ($headers as $key => $value) {
if(strtolower(trim($key)) == 'authorization') {
$token = trim($value[0]);
break;
}
}
$products = json_decode(Yii::$app->request->post('products'));
$post_products = Yii::$app->request->post('products');
if('111' == $token) {
if(isset($post_products) && $products) {
foreach ($products as $product) {
echo $product->price." = ".$product->productId."<br>";
Yii::$app->db->createCommand("UPDATE oc_product SET quantity = '" . (int)$product->quantity . "', price = '" . (float)$product->price . "' WHERE product_id = '" . (int)$product->productId . "'")->execute();
}
$json['success'] = 'complete';
} else {
$json['error'] = 'empty data';
}
} else {
$json['error'] = 'authorization error';
}
Yii::$app->controller->enableCsrfValidation = false;
echo json_encode($json);
}
I expect that data in my database will be updated by this controller. But there in nothing changes.
What do I do wrong? Maybe I should send some another headers? Thanks a lot )

Redirect in cannot be found page when I does click on save button from module

Page redirect in cannot be found! when, I does click on save button from module in admin OpenCart 2.1.0.1.
I think, module save button code is correct. but, Here, I can not understand. what is issue in my custom module. I have double check.
Please See following screenshot. when, I do click on save button from module in admin. So, It does redirect in "The page you requested cannot be found!" front side. but, page URL is still admin.
My admin module Controller File code
<?php
class ControllerModuleMytheme extends Controller {
private $error = array();
public function index() {
$language = $this->load->language('module/mytheme');
$data = array_merge($language);
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('setting/setting');
$this->load->model('tool/image');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$this->model_setting_setting->editSetting('mytheme', $this->request->post);
$this->session->data['success'] = $this->language->get('text_success');
$this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
}
$data['text_image_manager'] = 'Image manager';
$data['token'] = $this->session->data['token'];
$text_strings = array(
'heading_title',
'text_enabled',
'text_disabled',
'text_content_top',
'text_content_bottom',
'text_column_left',
'text_column_right',
'entry_status',
'entry_sort_order',
'button_save',
'button_cancel',
);
foreach ($text_strings as $text) {
$data[$text] = $this->language->get($text);
}
// store config data
$config_data = array(
//Status
'mytheme_status',
'mytheme_skin',
//Body Background
'mytheme_background_color',
'mytheme_button_color',
'mytheme_button_hover_color',
'mytheme_button_text_color',
);
foreach ($config_data as $conf) {
if (isset($this->request->post[$conf])) {
$data[$conf] = $this->request->post[$conf];
} else {
$data[$conf] = $this->config->get($conf);
}
}
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_module'),
'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('module/mytheme', 'token=' . $this->session->data['token'], 'SSL')
);
$data['action'] = $this->url->link('module/mytheme', 'token=' . $this->session->data['token'], 'SSL');
$data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
if (isset($this->request->post['mytheme_module'])) {
$modules = explode(',', $this->request->post['mytheme_module']);
} elseif ($this->config->get('mytheme_module') != '') {
$modules = explode(',', $this->config->get('mytheme_module'));
} else {
$modules = array();
}
if (isset($this->request->post['mytheme_status'])) {
$data['mytheme_status'] = $this->request->post['mytheme_status'];
} else {
$data['mytheme_status'] = $this->config->get('mytheme_status');
}
$this->load->model('localisation/language');
$data['languages'] = $this->model_localisation_language->getLanguages();
$data['modules'] = $modules;
if (isset($this->request->post['mytheme_module'])) {
$data['mytheme_module'] = $this->request->post['mytheme_module'];
} else {
$data['mytheme_module'] = $this->config->get('mytheme_module');
}
$data['mytheme_modules'] = array();
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('module/mytheme.tpl', $data));
}
protected function validate() {
if (!$this->user->hasPermission('modify', 'module/mytheme')) {
$this->error['warning'] = $this->language->get('error_permission');
}
return !$this->error;
}
}
What could be the reason?
Any clue?
There is often a problem with & in url encoding but it seems from your screenshot that it is already ok.
so everything looks fine, but you may have changed the name of the admin's URL so it shows the "NOT FOUND" page in "/admin".

OpenCart - View alternate product template based on arbitrary product field

There is another post on Stack Overflow that includes the following code for serving multiple product templates based on product ID
//42 is the id of the product
if ($this->request->get['product_id'] == 42) {
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/customproduct.tpl')) {
$this->template = $this->config->get('config_template') . '/template/product/customproduct.tpl';
} else {
$this->template = 'default/template/product/customproduct.tpl';
}
} else {
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/product.tpl')) {
$this->template = $this->config->get('config_template') . '/template/product/product.tpl';
} else {
$this->template = 'default/template/product/customproduct.tpl';
}
}
I would like to check for an alternate product field value that I won't be using instead of ID so it is something that can be managed from the admin panel.
For example, a statement that reads "If product location = accessory then get product/accessory.tpl"
Would I have to load that field in the product controller before I can request it with the if statement?
What would the syntax look like?
You should be able to use any of the fields in product data in the admin panel such as Location that you already referenced.
Everything from the product table for your requested row should be present in the $product_info array.
Try something like this:
$template = ($product_info['location'] == 'accessory') ? 'accessory.tpl' : 'product.tpl';
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/' . $template)) {
$this->template = $this->config->get('config_template') . '/template/product/' . $template;
} else {
$this->template = 'default/template/product/' . $template;
}
If you anticipate there will be many different templates for different locations it would be more efficient to use a switch control.
switch ($product_info['location']):
case 'accessory':
$template = 'accessory.tpl';
break;
case 'tool':
$template = 'tool.tpl';
break;
default:
$template = 'product.tpl';
break;
endswitch;
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/' . $template)) {
$this->template = $this->config->get('config_template') . '/template/product/' . $template;
} else {
$this->template = 'default/template/product/' . $template;
}
Hope that helps.

Opencart: CSS (based on route maintenance)

This is my first question here. I'm using Opencart for my webshop. For the looks i'm styling the CSS of my pages e.g. account_login.css. But i also want to do the Maintenance page. I already made the common_maintenance.css file. But when i'm testing my site isn't displaying correct. It is just showing http://www.mywebsite.com instead of http://www.mywebsite.com/index.php?route=common/maintenance. Please can somebody help me with this?
<?php
class ControllerCommonMaintenance extends Controller {
public function index() {
if ($this->config->get('config_maintenance')) { $route = '';
if (isset($this->request->get['route'])) {
$part = explode('/', $this->request->get['route']);
if (isset($part[0])) {
$route .= $part[0];
} }
// Show site if logged in as admin $this->load->library('user');
$this->user = new User($this->registry);
if (($route != 'payment') && !$this->user->isLogged()) {
return $this->forward('common/maintenance/info'); }
}
}
public function info() {
$this->load->language('common/maintenance');
$this->document->setTitle($this->language->get('heading_title'));
$this->data['heading_title'] = $this->language->get('heading_title');
$this->document->breadcrumbs = array();
$this->document->breadcrumbs[] = array(
'text' => $this->language->get('text_maintenance'), 'href' => $this->url->link('common/maintenance'),
'separator' => false
);
$this->data['message'] = $this->language->get('text_message');
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') .
'/template/common/maintenance.tpl')) {
$this->template = $this->config->get('config_template') . '/template/common/maintenance.tpl';
} else {
$this->template = 'default/template/common/maintenance.tpl'; }
$this->children = array( 'common/footer', 'common/header' );
$this->response->setOutput($this->render());
} } ?>
I've fixed the hideous formatting, and added the call to the ->addStyle($css). If the code does not work yet, please check if I got the path to the CSS right (where it says $css = 'blahblah' );
<?php
class ControllerCommonMaintenance extends Controller {
public function index() {
if ($this->config->get('config_maintenance')) {
$route = '';
if (isset($this->request->get['route'])) {
$part = explode('/', $this->request->get['route']);
if (isset($part[0])) {
$route .= $part[0];
}
}
// Show site if logged in as admin
// $this->load->library('user');
$this->user = new User($this->registry);
if (($route != 'payment') && !$this->user->isLogged()) {
return $this->forward('common/maintenance/info');
}
}
}
public function info() {
$this->load->language('common/maintenance');
$this->document->setTitle($this->language->get('heading_title'));
$css = 'catalog/view/theme/' . $this->config->get('config_template') . '/stylesheet/common_maintenance.css';
$this->document->addStyle($css);
$this->data['heading_title'] = $this->language->get('heading_title');
$this->document->breadcrumbs = array();
$this->document->breadcrumbs[] = array(
'text' => $this->language->get('text_maintenance'),
'href' => $this->url->link('common/maintenance'),
'separator' => false
);
$this->data['message'] = $this->language->get('text_message');
$maintenance = $this->config->get('config_template') . '/template/common/maintenance.tpl';
if (file_exists(DIR_TEMPLATE . $maintenance ) ) {
$this->template = $maintenance;
}
else {
$this->template = 'default/template/common/maintenance.tpl'; }
$this->children = array(
'common/footer',
'common/header'
);
$this->response->setOutput($this->render());
}
}
}
The problem is you're using the route, which the maintenance mode doesn't change when it's in maintenance mode. What you need to do is specifically add that stylesheet in the maintenance controller using the $this->document->addStyle() method

Categories