Tuesday, December 29, 2015

Prestashop set multiple select from the module and get them in the input

Question:
I am doing a small module in Prestashop. In that module I have used multiselect with helperform. So my code is like this
array(
    'type' => 'select',
    'cols' => 8,
    'class' => 'chosen-product-selct selected_products ',
    'multiple' => true,
    'label' => $this->l('Selected Products'),
    'name' => 'selected_products[]',
    'options' => array(
        'query' => $product_query,
        'id' => 'id',
        'name' => 'product_name'
    ),
    'desc' => $this->l('Select products from products list.'),
),
Here I am saving those multiselected values to the database. But when I am doing edit no saved values has been selected in the box. The box is totally empty.
for getting the result I am doing this
public function getConfigFieldsValues() {
    'selected_products[]'  => Tools::getValue('selected_products', Configuration::get('selected_products')),
}
Its not showing the values that has been entered.So can someone tell me how to solve this issue? Any help and suggestions will be really appreciable. Thanks
Amswer:
I found a solution to the problem with Prestashop multiple select form helper issue.
In order for the select box to work properly there are the following requirements:
  1. Input name should have '[]'. For example: manufacturer_ids[]
  2. $fields_value array should have this element with the same name: manufacturer_ids[] not manufacturer_ids.
  3. The value of the $fields_value array's corresponding item should have selected values as array. For example:
    $fields_value['manufacturer_ids[]'] = array("120", "145");
I save the submitted values as sarialized string in Database, so I am handling it this way:
    $selectedManufacturers = @unserialize($manufacturerIdsTakenFromDb);
    if ($selectedManufacturers === false && $selectedManufacturers !== 'b:0;') {
        $selectedManufacturers = array();
    }        
    $fields_value['manufacturer_ids[]'] = $selectedManufacturers;
And my code in form definition looks like this:
array(
    'type' => 'select',
    'label' => $this->l('Manufacturers'),
    'name' => 'manufacturer_ids[]',
    'multiple' => true,
    'options' => array(
        'query' => array(
            array('key' => '1', 'name' => 'Apple'),
            array('key' => '2', 'name' => 'Samsung'),
            array('key' => '3', 'name' => 'HTC'),
        ),
        'id' => 'key',
        'name' => 'name'
    )
),
If your code does satisfy all these and it still does not work, please let me know, I will be happy to assist you to fix it.

No comments:

Post a Comment