Programmers Direct Posted December 31, 2015 Share Posted December 31, 2015 (edited) Make Checkboxes in Prestashop Using helpers I was working on a module and I noticed that there is a barrier to doing checkboxes in Prestashop using the helpers they provide. I did however, find some help through the watermark module created by Prestashop that uses checkboxes as directed by some on the Prestashop forum see it by clicking here -> https://www.prestashop.com/forums/topic/3516[spam-filter]show-helper-form-checkbox-checked/. Now it seemed quite complex and I believe this may become over cumbersome for those who do not develop regularly. so I decided to create this tutorial to help others in their developing endeavors. ***This tutorial assumes you already know how to create a form using the helpers and can add all the required information to build around the input array you see below. You can start by adding the check box input array below array( 'type' => 'checkbox', 'label' => $this->l('Options'), 'desc' => $this->l('Choose options to use during regeneration or generation of product references. The module processes like this-> (Reference will equal or be generated to be as follows with | separating each section of the custom inputs and the product based information.) Reference -> customidentifier|customsymbolpos1|(random number from 1-999)|customsymbolpos2|itemid|itemcategoryid|customsymbolpos3|itemsupplierid|customsymbolpos4|current reference only **NOT Supplier reference <---. Please leave custom fields blank if you want to not provide any custom identifiers or characters.'), 'name' => 'options', 'values' => array( 'query' => $this->getOptions(), 'id' => 'id_checkbox_options', 'name' => 'checkbox_options_name', 'expand' => array( 'print_total' => count($this->getOptions()), 'default' => 'show', 'show' => array('text' => $this->l('show'), 'icon' => 'plus-sign-alt'), 'hide' => array('text' => $this->l('hide'), 'icon' => 'minus-sign-alt') ), ), ), After that we need to create the query for the checkboxes available. public function getOptions() { $options = array ( array ( 'id_checkbox_options' => 0, 'checkbox_options_name' => 'Option 0'), array ( 'id_checkbox_options' => 1, 'checkbox_options_name' => 'Option 1'), array ( 'checkbox_options_options' => 2, 'checkbox_options_name' => 'Option 2'), array ( 'checkbox_options_options' => 3, 'checkbox_options_name' => 'Option 3'), array ( 'id_checkbox_options' => 4, 'checkbox_options_name' => 'option 4') ); return $options; } Now we get and save the information from checkboxes after posting or saving the form. private function _postProcess() { $output = ''; if (Tools::isSubmit('submitBtn')) { Configuration::updateValue('checkbox_text_demo', Tools::getValue('checkbox_text_demo')); $all_opts = $this->getOptions(); $checkbox_options = array(); foreach ($all_opts as $chbx_options) if (Tools::getValue('options_'.(int)$chbx_options['id_checkbox_options'])) $checkbox_options[] = $chbx_options['id_checkbox_options']; Configuration::updateValue('options', implode(',', $checkbox_options)); $output .= $this->displayConfirmation($this->l('Settings updated')); } } Lastly, we load the submitted information into checkboxes as we load the form along with other field variables. protected function getConfigFormValues() { $config_fields = array ( 'checkbox_text_demo' => Configuration::get('checkbox_text_demo'), // if you have other fields to fill this would fill text input field. ); //get all checkbox stuff all available $opts = $this->getOptions(); $id_checkbox_options = array(); foreach ($opts as $options) $id_checkbox_options[] = $options['id_checkbox_options']; //get checkbox stuff from $_POST $id_checkbox_options_post = array(); foreach ($id_checkbox_options as $opt_id) if (Tools::getValue('options_'.(int)$opt_id)) $id_checkbox_options_post['options_'.(int)$opt_id] = true; //get checkbox stuff from Configuration $id_checkbox_options_config = array(); if ($confs = Configuration::get('options')) $confs = explode(',', Configuration::get('options')); else $confs = array(); foreach ($confs as $conf) $id_checkbox_options_config['options_'.(int)$conf] = true; //return only common values and value from post if (Tools::isSubmit('submit_Btn')) $config_fields = array_merge($config_fields, array_intersect($id_checkbox_options_post, $id_checkbox_options_config)); else $config_fields = array_merge($config_fields, $id_checkbox_options_config); return $config_fields; } That's it! Edited December 31, 2015 by razaro (see edit history) 3 Link to comment Share on other sites More sharing options...
امیر Posted April 23, 2017 Share Posted April 23, 2017 How can I change the add to cart in moudle related products got to my checking box example : Link to comment Share on other sites More sharing options...
امیر Posted April 25, 2017 Share Posted April 25, 2017 plz help Link to comment Share on other sites More sharing options...
Programmers Direct Posted April 25, 2017 Author Share Posted April 25, 2017 If you want to change the module and where it add the product you also have to change the attributes box and the page your one as well as adding js to allow it to update automatically and show the item on add look at how it is done in those pages and you can recreate it doing what you wish with examples to find. 1 Link to comment Share on other sites More sharing options...
امیر Posted April 26, 2017 Share Posted April 26, 2017 i not understant Link to comment Share on other sites More sharing options...
Mcey Posted May 18, 2018 Share Posted May 18, 2018 Hello, The tuto is very helpful. But I can't get the part the checked box is checked after we load the submitted. Can you give more details on the process to get the values of the checkbox field and put the checked please ? Thanks. Link to comment Share on other sites More sharing options...
RLdru Posted March 15, 2021 Share Posted March 15, 2021 If you want to load checked box with the getConfigFormValues() function, you should add in the displayForm() function (after the $helper = new HelperForm();): // $existedValues = $this->getConfigFormValues(); foreach($existedValues as $key=> $value) { $helper->fields_value[$key] = true; } // If you do a var_dump on the value return by getConfigFormValues(), you can see it's an array with the named input checkbox (and only the ones who's in the db of course). Bonne journée! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now