Author Topic: How to extend controllers and model for new template.  (Read 6625 times)

yonghan

  • Guest
How to extend controllers and model for new template.
« on: June 03, 2014, 08:20:22 AM »
Hi,i'm currently trying to convert html template to abantecart themes,i have used developer tools to create new theme,what i would like to ask is how to extend the controller and the model from new template.I have tried to copy the controller and the model for the storefront from default_html5 theme to the new theme,but it doesn't work.Hopefully abantecart would teach how to extend it.Thanks a lot.
« Last Edit: July 25, 2014, 02:44:17 AM by yonghan »

Offline abantecart

  • Administrator
  • Hero Member
  • *****
  • Posts: 3970
  • Karma: +243/-9
    • View Profile
    • Ideal Open Source Ecommerce Solution
Re: How to extend contollers and model for new template.
« Reply #1 on: June 03, 2014, 09:07:13 AM »
I doubt that you need to extend controllers or models heavily for the template. Possibly something small.

You can do one of the following:
1. Create a hook to controller
2. Create "pre" or "post" controller
Note: You can not override controller completely.

For #1. You need to create an Extension type class with "magic" hook method inside of main.php of your extension.
(This can be done in included file of cause)

Example:

Code: [Select]
class ExtensionTestName extends Extension {

 public function onControllerPagesCheckoutPayment_InitData() {
      $that = $this->baseObject;
 }

}

This class name should consist of Extension[extension text id] and shoudl extend Extension base class
To hook to specific controller, you need to create a "magic" method that id constructed as following:
onController[controller name path]_InitData()
or
onController[controller name path]_UpdateData()

InitData() is before main controller is executed and UpdateData() after main controller is executed.

To access and change main controller objects and data use $this->baseObject;

For #2.
You need to create a new controller with the same relative path as main controller starting from [ext dir]/[storefront oe admin]/controllers/....

Name of the controller should end with .pre or .post.
Example:  ../checkout/payment.pre.php

As for model. You can not extend model easily. You can always create your own model and get data what you need for your controller or hook.

This document covers extension dev in details:
http://www.abantecart.com/document_wiki/index.php/AbanteCart_Extension%E2%80%99s_Developer_Guide

If you have suggestions how to improve this document, please let us know. Help us to improve it.
We need your help to build better free open source eCommerce platform for everyone.
See how you can help

yonghan

  • Guest
Re: How to extend contollers and model for new template.
« Reply #2 on: June 04, 2014, 06:03:41 AM »
I have another question.Is it possible to get variables from controller via hook?Thanks

Offline abantecart

  • Administrator
  • Hero Member
  • *****
  • Posts: 3970
  • Karma: +243/-9
    • View Profile
    • Ideal Open Source Ecommerce Solution
Re: How to extend contollers and model for new template.
« Reply #3 on: June 05, 2014, 09:02:27 PM »
Yes. You can access anything that is present in view with t_UpdateData hook. See below example:

Code: [Select]
        public function onControllerPagesCheckoutPayment_UpdateData() {
                $that = $this->baseObject;

                echo_array($that->data['payment_methods']);
        }

You can try
Code: [Select]
echo_array($that->data);
We need your help to build better free open source eCommerce platform for everyone.
See how you can help

yonghan

  • Guest
Re: How to extend contollers and model for new template.
« Reply #4 on: June 05, 2014, 09:07:34 PM »
Hi abantecart,does hook can only access the view objects only and not able to access the base controller objects? Like get $results=$this->model_catalog_product->getProductsByCategoriesId(),what i would like to do is add isotope filter to category page and display products that belong to category and subcategory,so customer doesn't to click the subcategory and redirect to other page.Thanks
« Last Edit: June 05, 2014, 09:27:03 PM by yonghan »