How to Create a Custom Admin Page in 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 create

admin/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.

I found out how to do this. OpenCart uses the MVC pattern. I recommend reading about How to be an OpenCart Guru? post about learning how the system works - this Admin workflow should also suffice for customer end.

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

admin menu > Setting> user group> enable your controller

after that you can open your new page by this pattern

[admin url]/index.php?route=catalog/recipe

Permission Denied in admin custom page opencart

Here is the way add custom helloworld module in admin

  1. create admin/controller/custom/helloworld.php

    <?php
    class ControllerCustomHelloworld extends Controller {

    public function index() {

    $this->load->language('custom/helloworld');
    $this->document->setTitle($this->language->get('heading_title'));
    $this->load->model('custom/helloworld');

    $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('custom/helloworld.tpl', $data));

    }
    }
    ?>
  2. Create admin/model/custom/helloworld.php

    <?php
    class ModelCustomHelloworld extends Model {

    public function helloworldmodel(){

    }
    }
    ?>
  3. create admin/language/english/custom/helloworld.php

    <?php
    // Heading
    $_['heading_title'] = 'Hello world Admin module';

    ?>
  4. Create admin/view/template/custom/helloworld.tpl

    <?php echo $header; ?><?php echo $column_left; ?>
    <h1><?php echo "This is helloworld admin module in opencart 2.x.x.x "; ?></h1>
    <?php echo $footer; ?>
  5. Go to system -> users -> user groups -> edit Administrator group. Select all to access permission and modify permission and save.Sample Image

Here is the tutorial http://blog.a2bizz.com/index.php/2015/12/17/create-custom-module-in-opencart-admin/

How to create a static About us page in opencart

Step 1 : Goto information link in catalog tab

go to information link in catalog tab

Step 2 : click to insert a page or information page like about us

click to insert a page or information like about us

Step 3 : if you have enabled seo then you can write keyword for you aboutus page or information page then your url will be easy to use e.g below if seo is disbaled then your url will be little boring e.g(index.php?route=information/information&information_id=4)or simpler to it which will not be good to share or use

if you have seo enable then your about us will be aboutus or aboutus.html

NOTE: for seo you should use .htaccess file

Create a custom information page in OpenCart 1.5

After much research I discovered that the layouts don't work as expected. Luckily I had vQmod installed which I recommend everyone install!

Using vQmod I was able to modify the information/information template to target the layout id (in my case 20) and add the custom code. For a complete answer I have added the full vQmod code below.

<modification>
<id>Warehouse Registration</id>
<version>1</version>
<vqmver>1</vqmver>
<author>Matthew Bagley [paramiliar.com]</author>

<file name="catalog/controller/information/information.php">
<operation error="skip">
<search position="replace"><![CDATA[
$this->document->setTitle($information_info['title']);
]]></search>
<add><![CDATA[
$this->data['catid'] = $this->model_catalog_information->getInformationLayoutId($information_id);
]]></add>
</operation>
</file>
<file name="catalog/view/theme/*/template/information/information.tpl">
<operation error="skip">
<search position="after"><![CDATA[
<div class="page-description">
]]></search>
<add><![CDATA[
<? if ($catid == 20){?>
<div style="float: left; width: 48%; margin-right: 1%;">
<? } ?>
]]></add>
</operation>
<operation error="skip">
<search position="after"><![CDATA[
<?php echo $description; ?>
]]></search>
<add><![CDATA[
<? if ($catid == 20){?>
</div>
<div style="float: left; width: 48%; margin-right: 1%;">Registration form is here</div>
<? } ?>
]]></add>
</operation>
</file>
</modification>

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>


Related Topics



Leave a reply



Submit