Search code examples
symfonytwigprestashopsymfony4prestashop-1.7

add custom button on customer view twig using custom module in prestashop?


Firstly, I tried all the questions & answers related to this topic. Additionally and I tried related questions and try to solve it but no success. So please read my question thoroughly.

i want to add custom Button on Customer View page on prestashop( Version 1.7.6.1 )using my Custom module.

i found twig file : "personal_information.html.twig"

file located : "prestashop/src/prestashopModule/Resources/views/Admin/shell/Customer/view/personal_information.html.twig"

i attach screen short i want to put button.

enter image description here

Custom Module [myModule]

<?php


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

class MyCustomModule extends  PaymentModule
{
    public function __construct()
    {
        $this->name = 'MyCustomModule';
        $this->tab = 'payments XYZ';
        $this->version                = '1.0';
        $this->author                 = 'XYZ Technologies';
        $this->bootstrap              = true;
        $this->displayName            = 'XYZ';
        $this->description            = 'XYZ.';
        $this->confirmUninstall       = 'Are you sure you want to uninstall XYZ module?';
        $this->ps_versions_compliancy = array('min' => '1.7.0', 'max' => _PS_VERSION_);
        $this->allow_countries        = array('CH', 'LI', 'AT', 'DE');
        $this->allow_currencies       = array('CHF', 'EUR');

        parent::__construct();

    }

    /**
    * Install this module and register the following Hooks:
    *
    * @return bool
    */
    public function install()
    {
        if (Shop::isFeatureActive()) {
            Shop::setContext(Shop::CONTEXT_ALL);
        }

        Db::getInstance()->execute('
                CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'MyCustomModule` (
                `id` int(11) NOT NULL AUTO_INCREMENT,
                `customer_id` int(255) NOT NULL,
                `MyCustomModule` int(255) DEFAULT NULL,
                `lastcheck_date` date,
                `add_date` date,
                PRIMARY KEY (`id`)
        ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
        ');

        return parent::install() && $this->registerHook('Statusbtnoncustomerview');
    }
    /**
     * Uninstall this module and remove it from all hooks
     *
     * @return bool
     */
    public function uninstall()
    {
        return parent::uninstall() && $this->uninstallDb() && $this->unregisterHook('Statusbtnoncustomerview');
    }

    public function uninstallDb()
    {
        return Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'MyCustomModule');
    }

    public function hookStatusbtnoncustomerview()
    {
        /**
         * Verify if this module is enabled
         */
        if (!$this->active) {
            return;
        }
        return $this->fetch('module:MyCustomModule/views/templates/hook/personal_information.html.twig');
    }
    /**
     * Returns a string containing the HTML necessary to
     * generate a configuration screen on the admin
     *
     * @return string
     */
    public function getContent()
    {
        $output = null;
        if (Tools::isSubmit('submit'.$this->name)) {

            // get configuration fields value
            $MyCustomModule_Account_Data = strval(Tools::getValue('MyCustomModule_Account_Data'));
            $credit_Checkbox =  strval(Tools::getValue('credit_Checkbox_1'));
            $interval_Month = strval(Tools::getValue('Interval_Month'));

            if (
                !$MyCustomModule_Account_Data ||
                empty($MyCustomModule_Account_Data) ||
                !Validate::isGenericName($MyCustomModule_Account_Data)
            ) {
                $output .= $this->displayError($this->l('Please Enter MyCustomModule Account Data.'));
            } else{

                // Update configuration fields value
                Configuration::updateValue('MyCustomModule_Account_Data', $MyCustomModule_Account_Data);
                Configuration::updateValue('credit_Checkbox_1', $credit_Checkbox);
                Configuration::updateValue('Interval_Month', $interval_Month);


                // Display message after successfully submit value
                $output .= $this->displayConfirmation($this->l('Settings updated'));
            }
        }

        return $output.$this->displayForm();
    }
    /**
     * Display a form
     *
     * @param array $params
     * @return form html using helper form
     */
    public function displayForm()
    {
        // Get default language
        $defaultLang = (int)Configuration::get('PS_LANG_DEFAULT');
        $credit_Checkbox = [
            [
                'id'=>1,
                'name'=>'',
                'val' => 1
            ]
        ];
        // Init Fields form array
        $fieldsForm[0]['form'] = [
            'legend' => [
                'title' => $this->l('Configuration'),
            ],
            'input' => [
                [
                    'type' => 'text',
                    'label' => $this->l('MyCustomModule Account Data'),
                    'name' => 'MyCustomModule_Account_Data',
                    'required' => true
                ],
                [

                    'type'=>'checkbox',
                    'label'=> $this->l('credit'),
                    'name'=>'credit_Checkbox',
                    'values'=>[
                        'query'=>$credit_Checkbox,
                        'id'=>'id',
                        'name'=>'name'
                    ]

                ],
                [
                    'type' => 'html',
                    'html_content' => '<input type="number" min="0" step="1" value="'.Configuration::get('Interval_Month').'" name="Interval_Month">',
                    'label' => $this->l('interval Month'),
                    'name' => 'Interval_Month',
                    'size' => 20
                ],
            ],
            'submit' => [
                'title' => $this->l('Save'),
                'class' => 'btn btn-default pull-right'
            ]
        ];


        $helper = new HelperForm();

        // Module, token and currentIndex
        $helper->module = $this;
        $helper->name_controller = $this->name;
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;

        // Language
        $helper->default_form_language = $defaultLang;
        $helper->allow_employee_form_lang = $defaultLang;

        // Title and toolbar
        $helper->title = $this->displayName;
        $helper->show_toolbar = true;        // false -> remove toolbar
        $helper->toolbar_scroll = true;      // yes - > Toolbar is always visible on the top of the screen.
        $helper->submit_action = 'submit'.$this->name;
        $helper->toolbar_btn = [
            'save' => [
                'desc' => $this->l('Save'),
                'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
                    '&token='.Tools::getAdminTokenLite('AdminModules'),
            ],
            'back' => [
                'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
                'desc' => $this->l('Back to list')
            ]
        ];

        // Load current value
        $helper->fields_value['MyCustomModule_Account_Data'] = Configuration::get('MyCustomModule_Account_Data');
        $helper->fields_value['credit_Checkbox_1'] = Configuration::get('credit_Checkbox_1');
        $helper->fields_value['Interval_Month'] = Configuration::get('Interval_Month');


        return $helper->generateForm($fieldsForm);
    }


}

Solution

  • Since Prestashop 1.7 you can override Back Office views from within your own module.

    This means you only need to create:

    modules/mycustommodule/Admin/shell/Customer/view/personal_information.html.twig
    

    Then you should extend the original template in order to override it properly. You can copy the whole template and after that just add the html needed for your button. Hope it helps you can find all the documentaion here: https://devdocs.prestashop.com/1.7/modules/concepts/templating/admin-views/