Jump to content
Riya92

Adding multiselect dropdown in Admin customer details form in prestashop

Recommended Posts

I have added a multiselect dropdown field in customer adding form in admin. I have created new field in database. But I am not getting how to store the value of this new field to database. Please help

I have added this code to AdminCustomersController.php

array(

'type' => 'select',
'multiple' => true,
'label' => $this->l('Branch Location'),
'name' => 'branch_location',
'required' => false,
'options' => array(
'query' => $list_branch_arr,
'id' => 'branch_location',
'name' => 'name'
),
'col' => '4',
'hint' => array(
$this->l('Please choose the branch name from the branch list.')
)

Share this post


Link to post
Share on other sites

Thanks for your help. But I have already added the new field. But From multiple select dropdown ..it is only taking one value saved to database. I want to save all selected value from multiselect dropdown to database.  Please help.

Share this post


Link to post
Share on other sites

For a multiple select the name should end with [] ( eg. myselectname[] ). This will post an array of the selected values on submit.

In addition to Nemo's tutorial you should also extend the add() function to implode the select array into a csv string that will be saved into database, also in the constructor explode back to array.

  • Like 1

Share this post


Link to post
Share on other sites

When I am putting [] in end of field name it is showing error 'field is invalid'. Is there any way to solve this.

Share this post


Link to post
Share on other sites

The function that returns the the array with values for the fields ( the function getConfigFieldsValues() in most of default modules ) must have also [] at the end of that field key.

Share this post


Link to post
Share on other sites

I have simply added this in controller and it worked for me

 

 
if (Tools::isSubmit('submitAddcustomer'))
    {
 
 
$dept = Tools::getValue('department_or_branch');
$departments = implode(',', $dept);
$id = Tools::getValue('id_customer');
$sql = 'UPDATE zig_customer
SET department_or_branch="'.$departments.'"
WHERE id_customer='.$id;
 
$interested = Db::getInstance()->executeS($sql);
}

Share this post


Link to post
Share on other sites

hi! i have the same problem i want to store the input multiselect field as "1,2,3" in the table..

 

i noted that i must implote that values after the postprocess, but read here that i must extend the "add" how i do that? @gabdara

Share this post


Link to post
Share on other sites

i found the solution, due in this "forum comunity" there's no "comunity" only "forum sales"

 

THE COMPLETE CODE AND ALL THE STEPS ARE AT: https://groups.google.com/forum/m/?hl=es#!topic/venenuxsarisari/z8vfPsvFFjk

here i put only the most important parts..

 

as mentioned int he previous link, added a new fiel in the model definition, class and the table sql

 

this method permits to stored in the db as "1,2,3" so you can use only a single field to relation that multiple selected values, a better could be using groupbox but its quite difficult, take a look to the AdminCustomers controller class in the controllers directory of the prestachop, this has a multiselect group that used a realtional table event stored in single field

 

then in the helper form list array of inputs define a select as:

 

                array(
                     'type' => 'select',
                    'label' => $this->l('Select and employee'),
                    'name' => 'id_employee_tech',
                    'required' => false,
                    'col' => '6',
                    'default_value' => (int)Tools::getValue('id_employee_tech'),
                    'options' => array(
                        'query' => Employee::getEmployees(true), // el true es que solo los que estan activos
                        'id' => 'id_employee',
                        'name' => 'firstname',
                        'default' => array(
                            'value' => '',
                            'label' => $this->l('ninguno')
                        )
                    )
                ),
 

 

an then override the post process too

 

    public function postProcess()
    {
        if (Tools::isSubmit('submitTallerOrden'))
        {
            $_POST['id_employee'] = implode(',', Tools::getValue('id_employee'));
        }
        parent::postProcess();
    }

this make stored in the db as "1,2,3"

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More