Question:
How to override controllers in cs-cart?

Problem:

Sometimes you need to override default controller behavior in cs-cart. But you can't use override modifier like in hooks.

What are approaches we can use to override default controllers?


Solution:

The simplest approach I found is to use dispatch_assign_template hook from fn.control.php file.


  1. define hook in your addon's init.php file.

  2. add next function in your addon's func.php file

function fn_myaddon_dispatch_assign_template($controller, $mode, $area, &$controllers_cascade) {

    if ($controller !== 'categories' || $mode !== 'view') {

        return;

    }


    //excluded default categories controller 

    $default_controller_path  = DIR_ROOT . '/app/controllers/frontend/categories.php';

    

    $controllers_cascade = array_filter(

        $controllers_cascade,

        static function ($item) use ($default_controller_path) {

            return $default_controller_path !== $item;

        }

    );

}

  • We passed &$controllers_cascade parameter by reference.

  • We excluded default controller from $controllers_cascade array.


  1. Add your own controller like categories.post.php and add

if ($mode == 'view') {

    //your code here.

}


Answered by: >Anri

Credit: >Stack Overflow


Read more:

>How to Use RTK Queries in a React App?

>Step by Step guide to Deploy Terraform in Azure using GitHub Actions

>Integrate SASS into the build and consume a UI toolkit

>What are common syntax errors and exceptions in Python

>Creating API in Python using Flask

>How to solve XGBoost model training fails in Python

>How to make non-programmers to run a Python program?


Ritu Singh

Ritu Singh

Submit
0 Answers