Jump to content

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.')
)

Link to comment
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.

Link to comment
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
Link to comment
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);
}
Link to comment
Share on other sites

  • 1 year later...

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"

  • Like 1
Link to comment
Share on other sites

×
×
  • Create New...