Search code examples
phpcs-cart

How to override controllers in cs-cart


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.
    }