Jump to content

Helpfulcheckboxes  

3 members have voted

  1. 1. Was this tutorial helpful

    • Yes
      3
    • No
      0


Recommended Posts

11-single-default.jpg

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 by razaro (see edit history)
  • Like 3
Link to comment
Share on other sites

  • 1 year later...

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.

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

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

  • 2 years later...

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...