Warm tip: This article is reproduced from stackoverflow.com, please click
prestashop prestashop-1.7

Prestashop 1.7 override front controller from module

发布于 2020-05-02 22:17:17

I have problem with override front controller in my custom module. I have module:

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

class cartlimit extends Module
{

    public function __construct()
    {
        $this->name = 'cartlimit';
        $this->tab = 'front_office_features';
        $this->author = 'somedata';
        $this->version = '1.0.0';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('cart limit');
        $this->description = $this->l('module for cart limit');
    }

    public function install()
    {
        return parent::install();
    }

    public function uninstall()
    {
        return parent::uninstall();
    }
}

in my module i have controller override/controllers/CartController.php with code:

      <?php

use PrestaShop\PrestaShop\Adapter\Presenter\Cart\CartPresenter;

class CartControllerCore extends FrontController
{
    public $php_self = 'cart';

    public function init()
    {
        parent::init();
        $this->qty = abs(Tools::getValue('qty', 1));
        var_dump(1);

        if ($this->qty >= 2) {
            #How can i show notification?
        }
    }

}

When i install my module, and add product to cart, then my override not working. Presta adds product to cart instead show var_dump. Second question is, how can i show notification when $this->qty is >= 2 ?

I ask everywhere, but no one answer.

Questioner
PawelC
Viewed
384
OnklMaps 2020-02-13 22:48

You need to save in yourmodule/override/controllers/front/CartController.php.

Then you need to override the core CartController like this:

CartController extends CartControllerCore {
    // do whatever
}

Lastly you need to reset/reinstall module for PrestaShop to copy the override automatically.