Adding an admin page on OpenCart version 2
The difference between page rendering in OC < 2.0 and OC 2.0 are only few but you have to be aware of them.
1. $data
In OC < 2.0 you would do this:
$this->data['text_button_save'] = $this->language->get('text_button_save');
while in OC 2.0 it is only $data
, i.e.$data['text_button_save'] = $this->language->get('text_button_save');
that is passed over to the $this->load->view()
method as an argument, e.g.:$this->response->setOutput($this->load->view('catalog/category_list.tpl', $data));
2. $this->render()
is gone. Now you are calling $this->load->view('catalog/category_list.tpl', $data)
instead.
3. $this->children
is gone. Now the template child modules' positions are instantiated as part of template properties while you have to call their controllers manually (WHY?):
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
I was thinking why the hell were these changes required. What has been improved? Did they want the developers to write less code? Is it now more following the OOP, MVC, WTF (sorry) principles? And got the answer: NO (or nothing to the first one).We still have to load the translations (I mean, we still have to load each single string translation). And gettext
is out there for more than 8 years...
Instead of short $this->response->setOutput($this->render());
we now have to call much longer (and incomprehensible) $this->response->setOutput($this->load->view('catalog/category_form.tpl', $data));
. Why can't we just do this: $this->render('catalog/category_form.tpl', $data);
???
I personally think OC 2.0 is the same excrement (from the developers perspective) as it was before. They just changed the packaging. But, honestly, there are even bigger excrements out there, that's why I'm stuck with OpenCart :-)
How to create a custom admin page in opencart?
OpenCart 2.x
The path names have changed in OpenCart 2 - you will want to createadmin/controller/extension/module/hello.php
admin/language/en-gb/extension/module/hello.php
admin/view/template/extension/module/hello.tpl
Then the route becomes
admin/index.php?route=extension/module/hello
OpenCart 1.x
- Include full MVC flow.
1) Create a new file in admin/controller/custom/helloworld.php
Your filename and controller name should be the same in desc order:
helloworld.php
<?
class ControllerCustomHelloWorld extends Controller{
public function index(){
// VARS
$template="custom/hello.tpl"; // .tpl location and file
$this->load->model('custom/hello');
$this->template = ''.$template.'';
$this->children = array(
'common/header',
'common/footer'
);
$this->response->setOutput($this->render());
}
}
?>
2) Create a new file in admin/view/template/custom/hello.tpl
Hello.tpl
<?php echo $header; ?>
<div id="content">
<h1>HelloWorld</h1>
<?php
echo 'I can also run PHP too!';
?>
</div>
<?php echo $footer; ?>
3) Create a new file in admin/model/custom/hello.php
<?php
class ModelCustomHello extends Model {
public function HellWorld() {
$sql = "SELECT x FROM `" . DB_PREFIX . "y`)";
$implode = array();
$query = $this->db->query($sql);
return $query->row['total'];
}
}
?>
4) You then need to enable the plugin to avoid permission denied errors:Opencart > Admin > Users > User Groups > Admin > Edit
Select and Enable the Access Permission.To visit your page go to
www.yoursite.com/opencart/admin/index.php?route=custom/helloworld
adding new page to opencart admin
you need enable controller in admin panel to display your controller
after that you can open your new page by this patternadmin menu > Setting> user group> enable your controller
[admin url]/index.php?route=catalog/recipe
OpenCart problems adding new admin pages
Your server doesn't seem to be able to use php short tags. Change <?
to <?php
- Since PHP doesn't load that file as a PHP file, the class doesn't exist. Changing it to <?php
will get it working
Add new admin menu under catalog section in opencart
Because admin theme is generated to the modification template you should refresh your modification in Extension > Modifications > refresh button at the top right.
That's it.
Add a Custom Field on Opencart admin 'Order Info' page
I was able to figure this out myself. And this is what I understood. I maybe wrong but the simple code worked.
I was doing changes in Admin order section and it is important to figure out which method the change is being done in. For Model section, use apropriate method and add query or edit present query. Same with Controller. If you are trying to display as a list - order_list(getList()) or order_info(getInfo) section. It may be simple to people who are good at it but for me, this was my first, so it took lot of time.
below is the working code in VQMOD format.
<modification>
<id><![CDATA[custom order list]]></id>
<version>1</version>
<vqmver>2.X</vqmver>
<author>customAuthor</author>
<file name="admin/language/english/sale/order.php">
<operation>
<search position="after"><![CDATA[
$_['text_order_id'] = 'Order ID:';
]]></search>
<add><![CDATA[
$_['text_custom_order_number'] = 'custom:';
]]></add>
</operation>
<operation>
<search position="after"><![CDATA[
$_['column_order_id'] = 'Order ID';
]]></search>
<add><![CDATA[
$_['column_custom_order_number'] = 'custom <i class="fa fa-shopping-cart"></i>';
]]></add>
</operation>
</file>
<file name="admin/view/template/sale/order_list.tpl">
<operation>
<search position="after"><![CDATA[
<a href="<?php echo $sort_order; ?>"><?php echo $column_order_id; ?></a>
]]></search>
<add><![CDATA[
<!-- custom -->
<td class="text-left">
<?php echo $column_custom_order_number; ?></a>
</td>
<!-- custom -->
]]></add>
</operation>
<operation>
<search position="after"><![CDATA[
<td class="text-right"><?php echo $order['order_id']; ?></td>
]]></search>
<add><![CDATA[
<td class="text-left"><?php if(!empty($order['cu_orders'])){echo "CU".$order['cu_orders'];} else{echo " ";} ?></td>
]]></add>
</operation>
</file>
<file name="admin/view/template/sale/order_info.tpl">
<operation>
<search position="after" offset="1"><![CDATA[
<td>#<?php echo $order_id; ?></td>
]]></search>
<add><![CDATA[
<!-- Shopgate -->
<tr>
<td><?php echo $text_custom_order_number; ?></td>
<td><?php if (!empty($custom_order_number)) { ?>
<?php echo 'CU'.$custom_order_number; ?>
<?php } else { ?>
<?php echo " "; ?>
<?php } ?>
</td>
</tr>
<!-- Shopgate -->
]]></add>
</operation>
</file>
<file name="admin/model/sale/order.php">
<!-- getOrder() Modifications -->
<operation>
<search position="replace"><![CDATA[
(SELECT CONCAT(c.firstname, ' ', c.lastname) FROM " . DB_PREFIX . "customer c WHERE c.customer_id = o.customer_id) AS customer
]]></search>
<add><![CDATA[
(SELECT CONCAT(c.firstname, ' ', c.lastname) FROM " . DB_PREFIX . "customer c WHERE c.customer_id = o.customer_id) AS customer, (SELECT s.custom_order_number FROM " . DB_PREFIX . "custom_orders s WHERE s.custom_order_number = o.order_id) AS custom_order_number
]]></add>
</operation>
<operation>
<search position="after"><![CDATA[
'order_id' => $order_query->row['order_id'],
]]></search>
<add><![CDATA[
'custom_order_number' => $order_query->row['custom_order_number'],
]]></add>
</operation>
<!-- getOrderS() Modifications -->
<operation>
<search position="replace"><![CDATA[
CONCAT(o.firstname, ' ', o.lastname) AS customer,
]]></search>
<add><![CDATA[
CONCAT(o.firstname, ' ', o.lastname) AS customer, (SELECT s.custom_order_number FROM " . DB_PREFIX . "custom_orders s WHERE s.custom_order_number = o.order_id) AS custom_order_number,
]]></add>
</operation>
</file>
<file name="admin/controller/sale/order.php">
<!-- getList() Modifications -->
<operation>
<search position="after"><![CDATA[
'order_id' => $result['order_id'],
]]></search>
<add><![CDATA[
'cu_orders' => $result['custom_order_number'],
]]></add>
</operation>
<!-- getForm() Modifications -->
<operation>
<search position="after"><![CDATA[
$data['store_id'] = $order_info['store_id'];
]]></search>
<add><![CDATA[
$data['custom_order_number'] = $order_info['custom_order_number'];
]]></add>
</operation>
<!-- getInfo() Modifications -->
<operation>
<search position="after"><![CDATA[
$data['text_order_id'] = $this->language->get('text_order_id');
]]></search>
<add><![CDATA[
$data['text_custom_order_number'] = $this->language->get('text_custom_order_number');
]]></add>
</operation>
<operation>
<search position="after"><![CDATA[
$data['store_name'] = $order_info['store_name'];
]]></search>
<add><![CDATA[
$data['custom_order_number'] = $order_info['custom_order_number'];
]]></add>
</operation>
</file>
</modification>
Page level permissions requited in opencart admin
You need to call $this->user->user_group_id
from page controller.
If you try to call this from view, you will not get value, because in OpenCart $this
object is not completly passed to views.
Related Topics
Z-Scores(Standard Deviation and Mean) in PHP
Difference Between $_Server['Document_Root'] and $_Server['Http_Host']
How to Sort an Array by Similarity in Relation to an Inputted Word
Selenium2 Firefox: Use the Default Profile
How to Access Elements in an Array Returned from a Function
How to Remove Exif from a Jpg Without Losing Image Quality
Mail Not Sending with PHPmailer Over Ssl Using Smtp
How to Get Products from a Particular Category in Magento Ecommerce
Convert Windows Timestamp to Date Using PHP on a Linux Box
Calculate the Difference Between 2 Timestamps in PHP
Create a Joomla! Article Programmatically
Replace Any Url's Within a String of Text, to Clickable Links with PHP
File_Get_Contents('Php://Input') Always Returns an Empty String
Order Properties Should Not Be Accessed Directly - Woocommerce 3.0