简体   繁体   中英

Prestashop 1.7 override core controller from module

I am facing a strange situation. I am trying to override cart controller and I am able to succeed if I place the CartController.php file in /override/controllers/front and fails if I place the same file in my custom module and installing the module. The path of file in module is myModule/override/controllers/front . The file contents are

class CartController extends CartControllerCore
{
  public function init()
  {
    die('Override');
  }
}

I have also registered a hook and it displays fine.

  public function install()
  {
    if (Shop::isFeatureActive())
      Shop::setContext(Shop::CONTEXT_ALL);

    return parent::install() && $this->registerHook('Test');
  }

  public function uninstall()
  {
    if (!parent::uninstall() ||
      !Configuration::deleteByName('MYMODULE_NAME'))
      return false;
    return true;
  }

  public function hookTest($params){

    return $this->display(__FILE__, '/views/templates/hook/testpage.tpl');
  }

What am I missing here?

I believe you are doing everthing ok. The override in your module is copied to the override folder/file when you install the module, unless there is already another override for that class function. See this answer https://stackoverflow.com/a/24114184

Hi curious_coder all above are mentioned by you are correct,but you have missed how to override a file in corresponding folder.

Write below code in your cart custom module for override file.

protected static $overrides = array(
    'controllers/front/CartController.php'
);//Override file name

public function install(){
    if (Shop::isFeatureActive())
    Shop::setContext(Shop::CONTEXT_ALL);
    foreach(self::$overrides as $file){
        $explode = explode("/", $file);
        $file_name = $explode[count($explode)-1];
        unset($explode[count($explode)-1]);
        $folder = implode("/", $explode);
        @mkdir (_PS_OVERRIDE_DIR_.$folder, 0777, true);
        @copy ( _PS_MODULE_DIR_.$this->name.'/override/'.$folder."/".$file_name 
    , _PS_OVERRIDE_DIR_.$folder."/".$file_name );
        $old = @umask(0);
        @chmod (_PS_OVERRIDE_DIR_.$folder."/".$file_name, 0777);
        @umask($old);
    }
    return parent::install() && $this->registerHook('Test');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM