Bonjour,

Depuis plusieurs jours je me fracasse la tête sur ce script ou je n'arrive pas à trouver de solution.
Le problème est d'arriver à créer différents select html avec les bonnes valeurs.
Actuellement les select sont crées et dupliqués à chaque fois avec des valeurs qui s'ajoutent !
Pour créer une liste d'options de produit, cela ne fonctionne pas

Si avez une idées pour arriver le le bon rendu, je suis preneur (cf 2/)


Merci de votre aide.


Si vous avez besoins compléments d'information notamment de données, n'hésitez pas.

Comme vous pouvez le voir actuellement le rendu est comme ceci :

1/ rendu actuel : cf https://jsfiddle.net/8zbqxyqo/2/

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<div class="form-group">
<div class="form-group">
<label class="control-label" for="input-option">Couleur</label>
<select name="option[]" id="input-option" class="form-control">
<option value="">--- Selectioner ---</option>
<option value=""> Red() + : 50</option>
</select>
</div>
<div class="form-group">
<label class="control-label" for="input-option">Couleur</label>
<select name="option[]" id="input-option" class="form-control">
<option value="">--- Selectioner ---</option>
<option value=""> Red() + : 50</option>
<option value=""> Vert() </option>
</select>
</div>
<div class="form-group">
<label class="control-label" for="input-option">Taille</label>
<select name="option[]" id="input-option" class="form-control">
<option value="">--- Selectioner ---</option>
<option value=""> Red() + : 50</option>
<option value=""> Vert() </option>
<option value=""> S() </option>
</select>
</div>
<div class="form-group">
<label class="control-label" for="input-option">Taille</label>
<select name="option[]" id="input-option" class="form-control">
<option value="">--- Selectioner ---</option>
<option value=""> Red() + : 50</option>
<option value=""> Vert() </option>
<option value=""> S() </option>
<option value=""> M() + : 20</option>
</select>
</div>
</div>
2 / voici le bon rendu qui doit être généré :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<div class="form-group">
<label class="control-label" for="input-option">Couleur</label>
<select name="option[1]" id="input-option1" class="form-control">
<option value="">--- Selectioner ---</option>
<option value=""> Red() + : 50</option>
<option value=""> Vert() </option>
</select>
</div>
 
<div class="form-group">
<label class="control-label" for="input-option">Taille</label>
<select name="option[2]" id="input-option2" class="form-control">
<option value="">--- Selectioner ---</option>
<option value=""> S() </option>
<option value=""> M() + : 20</option>
</select>
</div>

Voici le code php en conséquence avec les informations complémentaires pour vous aider.

la fonction

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 
public function getProductOptions($product_id) {
      $QproductOption = $this->db->prepare('select * 
                                            from :table_test_products_option po left join :table_test_products_options_attributes o ON (po.option_id = o.option_id) 
                                                                                left join :table_test_products_options_attributes_description od ON (o.option_id = od.option_id) 
                                            where po.products_id = :products_id
                                            and od.language_id = :language_id
                                            order by o.sort_order
                                            ');
 
      $QproductOption->bindInt(':products_id', $product_id );
      $QproductOption->bindInt(':language_id', $this->lang->getId());
      $QproductOption->execute();
 
      $products_option = $QproductOption->fetchAll();
 
      foreach ($products_option as $key => $product_option) {
 
        $QproductOptionValue = $this->db->prepare('select * 
                                                   from :table_test_products_option_value pov left join :table_test_products_options_attributes_value ov ON (pov.option_value_id = ov.option_value_id) 
                                                                                              left join :table_test_products_options_attributes_value_description ovd ON (ov.option_value_id = ovd.option_value_id) 
                                                  where pov.products_id = :products_id
                                                  and pov.products_option_id = :products_option_id
                                                  and ovd.language_id = :language_id
                                                  order by ov.sort_order
                                                  ');
 
        $QproductOptionValue->bindInt(':products_id', $product_id );
        $QproductOptionValue->bindInt(':language_id', $this->lang->getId());
        $QproductOptionValue->bindInt(':products_option_id', $product_option['products_option_id']);
        $QproductOptionValue->execute();
 
        $product_option_value = $QproductOptionValue->fetchAll();
 
        foreach ($product_option_value as  $pov => $products_value) {
          $product_option_value_data[] = ['products_option_value_id' => $products_value['products_option_value_id'],
                                          'option_value_id'         => $products_value['option_value_id'],
                                          'name'                    => $products_value['name'],
                                          'image'                   => $products_value['image'],
                                          'quantity'                => $products_value['quantity'],
                                          'subtract'                => $products_value['subtract'],
                                          'price'                   => $products_value['price'],
                                          'price_prefix'            => $products_value['price_prefix'],
                                          'weight'                  => $products_value['weight'],
                                          'weight_prefix'           => $products_value['weight_prefix'],
                                          'customers_group_id'      => $products_value['customers_group_id'],
                                          'products_option_model'   => $products_value['products_option_model'],
                                          'option_tax_class_id'     => $products_value['option_tax_class_id']
                                        ];
        }
 
        $product_option_data[] = [
                                  'products_option_id'    => $product_option['products_option_id'],
                                  'option_id'            => $product_option['option_id'],
                                  'name'                 => $product_option['name'],
                                  'type'                 => $product_option['type'],
                                  'value'                => $product_option['value'],
                                  'required'             => $product_option['required'],
                                  'products_option_value' => $product_option_value_data
                                ];
      }
 
      return $product_option_data;
    }

résultat de $product_option_value_data


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
 
array (size=1)
  0 => 
    array (size=13)
      'products_option_value_id' => string '283' (length=3)
      'option_value_id' => string '187' (length=3)
      'name' => string 'Red' (length=3)
      'image' => null
      'quantity' => string '200' (length=3)
      'subtract' => string '0' (length=1)
      'price' => string '50.0000' (length=7)
      'price_prefix' => string '+' (length=1)
      'weight' => string '0.00' (length=4)
      'weight_prefix' => string '+' (length=1)
      'customers_group_id' => string '0' (length=1)
      'products_option_model' => string '' (length=0)
      'option_tax_class_id' => string '0' (length=1)
 
array (size=2)
  0 => 
    array (size=13)
      'products_option_value_id' => string '283' (length=3)
      'option_value_id' => string '187' (length=3)
      'name' => string 'Red' (length=3)
      'image' => null
      'quantity' => string '200' (length=3)
      'subtract' => string '0' (length=1)
      'price' => string '50.0000' (length=7)
      'price_prefix' => string '+' (length=1)
      'weight' => string '0.00' (length=4)
      'weight_prefix' => string '+' (length=1)
      'customers_group_id' => string '0' (length=1)
      'products_option_model' => string '' (length=0)
      'option_tax_class_id' => string '0' (length=1)
  1 => 
    array (size=13)
      'products_option_value_id' => string '282' (length=3)
      'option_value_id' => string '186' (length=3)
      'name' => string 'Vert' (length=4)
      'image' => null
      'quantity' => string '0' (length=1)
      'subtract' => string '0' (length=1)
      'price' => string '0.0000' (length=6)
      'price_prefix' => string '+' (length=1)
      'weight' => string '0.00' (length=4)
      'weight_prefix' => string '+' (length=1)
      'customers_group_id' => string '0' (length=1)
      'products_option_model' => string '' (length=0)
      'option_tax_class_id' => string '0' (length=1)
 
array (size=3)
  0 => 
    array (size=13)
      'products_option_value_id' => string '283' (length=3)
      'option_value_id' => string '187' (length=3)
      'name' => string 'Red' (length=3)
      'image' => null
      'quantity' => string '200' (length=3)
      'subtract' => string '0' (length=1)
      'price' => string '50.0000' (length=7)
      'price_prefix' => string '+' (length=1)
      'weight' => string '0.00' (length=4)
      'weight_prefix' => string '+' (length=1)
      'customers_group_id' => string '0' (length=1)
      'products_option_model' => string '' (length=0)
      'option_tax_class_id' => string '0' (length=1)
  1 => 
    array (size=13)
      'products_option_value_id' => string '282' (length=3)
      'option_value_id' => string '186' (length=3)
      'name' => string 'Vert' (length=4)
      'image' => null
      'quantity' => string '0' (length=1)
      'subtract' => string '0' (length=1)
      'price' => string '0.0000' (length=6)
      'price_prefix' => string '+' (length=1)
      'weight' => string '0.00' (length=4)
      'weight_prefix' => string '+' (length=1)
      'customers_group_id' => string '0' (length=1)
      'products_option_model' => string '' (length=0)
      'option_tax_class_id' => string '0' (length=1)
  2 => 
    array (size=13)
      'products_option_value_id' => string '281' (length=3)
      'option_value_id' => string '179' (length=3)
      'name' => string 'S' (length=1)
      'image' => null
      'quantity' => string '0' (length=1)
      'subtract' => string '0' (length=1)
      'price' => string '0.0000' (length=6)
      'price_prefix' => string '+' (length=1)
      'weight' => string '0.00' (length=4)
      'weight_prefix' => string '+' (length=1)
      'customers_group_id' => string '0' (length=1)
      'products_option_model' => string '' (length=0)
      'option_tax_class_id' => string '0' (length=1)
 
 
array (size=4)
  0 => 
    array (size=13)
      'products_option_value_id' => string '283' (length=3)
      'option_value_id' => string '187' (length=3)
      'name' => string 'Red' (length=3)
      'image' => null
      'quantity' => string '200' (length=3)
      'subtract' => string '0' (length=1)
      'price' => string '50.0000' (length=7)
      'price_prefix' => string '+' (length=1)
      'weight' => string '0.00' (length=4)
      'weight_prefix' => string '+' (length=1)
      'customers_group_id' => string '0' (length=1)
      'products_option_model' => string '' (length=0)
      'option_tax_class_id' => string '0' (length=1)
  1 => 
    array (size=13)
      'products_option_value_id' => string '282' (length=3)
      'option_value_id' => string '186' (length=3)
      'name' => string 'Vert' (length=4)
      'image' => null
      'quantity' => string '0' (length=1)
      'subtract' => string '0' (length=1)
      'price' => string '0.0000' (length=6)
      'price_prefix' => string '+' (length=1)
      'weight' => string '0.00' (length=4)
      'weight_prefix' => string '+' (length=1)
      'customers_group_id' => string '0' (length=1)
      'products_option_model' => string '' (length=0)
      'option_tax_class_id' => string '0' (length=1)
  2 => 
    array (size=13)
      'products_option_value_id' => string '281' (length=3)
      'option_value_id' => string '179' (length=3)
      'name' => string 'S' (length=1)
      'image' => null
      'quantity' => string '0' (length=1)
      'subtract' => string '0' (length=1)
      'price' => string '0.0000' (length=6)
      'price_prefix' => string '+' (length=1)
      'weight' => string '0.00' (length=4)
      'weight_prefix' => string '+' (length=1)
      'customers_group_id' => string '0' (length=1)
      'products_option_model' => string '' (length=0)
      'option_tax_class_id' => string '0' (length=1)
  3 => 
    array (size=13)
      'products_option_value_id' => string '280' (length=3)
      'option_value_id' => string '180' (length=3)
      'name' => string 'M' (length=1)
      'image' => null
      'quantity' => string '100' (length=3)
      'subtract' => string '0' (length=1)
      'price' => string '20.0000' (length=7)
      'price_prefix' => string '+' (length=1)
      'weight' => string '0.00' (length=4)
      'weight_prefix' => string '+' (length=1)
      'customers_group_id' => string '0' (length=1)
      'products_option_model' => string '' (length=0)
      'option_tax_class_id' => string '0' (length=1)


résultat de $product_option_data


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 
array (size=4)
  0 => 
    array (size=7)
      'products_option_id' => string '410' (length=3)
      'option_id' => string '42' (length=2)
      'name' => string 'Couleur' (length=7)
      'type' => string 'select' (length=6)
      'value' => null
      'required' => null
      'products_option_value' => 
        array (size=1)
          0 => 
            array (size=13)
              ...
  1 => 
    array (size=7)
      'products_option_id' => string '409' (length=3)
      'option_id' => string '42' (length=2)
      'name' => string 'Couleur' (length=7)
      'type' => string 'select' (length=6)
      'value' => null
      'required' => null
      'products_option_value' => 
        array (size=2)
          0 => 
            array (size=13)
              ...
          1 => 
            array (size=13)
              ...
  2 => 
    array (size=7)
      'products_option_id' => string '408' (length=3)
      'option_id' => string '40' (length=2)
      'name' => string 'Taille' (length=6)
      'type' => string 'select' (length=6)
      'value' => null
      'required' => null
      'products_option_value' => 
        array (size=3)
          0 => 
            array (size=13)
              ...
          1 => 
            array (size=13)
              ...
          2 => 
            array (size=13)
              ...
  3 => 
    array (size=7)
      'products_option_id' => string '407' (length=3)
      'option_id' => string '40' (length=2)
      'name' => string 'Taille' (length=6)
      'type' => string 'select' (length=6)
      'value' => null
      'required' => null
      'products_option_value' => 
        array (size=4)
          0 => 
            array (size=13)
              ...
          1 => 
            array (size=13)
              ...
          2 => 
            array (size=13)
              ...
          3 => 
            array (size=13)
              ...


génération du code côté front office :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 
          $data['options'] = [];
 
          $options_array = $this->ProductsAttributesShop->getProductOptions($OSCOM_ProductsCommon->getID());
 
          foreach ($options_array as $option) {
            $product_option_value_data = [];
 
            foreach ($option['products_option_value'] as $option_value) {
              if (isset($option_value['subtract']) || $option_value['quantity'] > 0) {
                  if ($option_value['price'] > 0) {
                    $price = (float)$option_value['price'];// inclure la taxe
                  } else {
                    $price = false;
                  }
 
                  $product_option_value_data[] =  ['products_option_value_id' => $option_value['products_option_value_id'],
                                                   'option_value_id'         => $option_value['option_value_id'],
                                                   'name'                    => $option_value['name'],
                                                   'image'                   => $option_value['image'],
                                                   'quantity'                => $option_value['quantity'],
                                                   'price'                   => $price,
                                                   'price_prefix'            => $option_value['price_prefix'],
                                                   'weight'                  => $option_value['weight'],
                                                   'weight_prefix'           => $option_value['weight_prefix'],
                                                   'customers_group_id'      => $option_value['customers_group_id'],
                                                   'products_option_model'   => $option_value['products_option_model'],
                                                  ];
              }
            }
 
            $data['options'][] = [
                                  'products_option_id'    => $option['products_option_id'],
                                  'option_id'            => $option['option_id'],
                                  'name'                 => $option['name'],
                                  'type'                 => $option['type'],
                                  'value'                => $option['value'],
                                  'required'             => $option['required'],
                                  'products_option_value' => $product_option_value_data
                                  ];
          }
 
          $products_options_content_display .= ' <div>';
 
//******************************************
// select
//******************************************
         $products_options_content_display .= '<div class="form-group">'; 
 
          if ($data['options']) {
            $test = '';
 
            foreach ($data['options'] as $key => $option) {
              if ($option['type'] == 'select') {
                if ($option['required'] == 1) $required = ' required';
 
                $test .= '<div class="form-group' . $required . '">';
                $test .= '<label class="control-label" for="input-option' . $option['product_option_id'] . '">' . $option['name'] . '</label>';
                $test .= '<select name="option[' . $option['product_option_id'] . ']" id="input-option' . $option['product_option_id'] . ' class="form-control">';
                $test .= '<option value="">' . OSCOM::getDef('text_select') . '</option>';
 
                foreach ($option['products_option_value'] as $t => $option_value) {
                  $test .= '<option value="' .  $option_value['product_option_value_id'] . '"> ' . $option_value['name'];
 
                  if (!is_null($option_value['products_option_model'])) {
                    $model = $option_value['products_option_model'];
                    $test .= '(' . $model . ') ';
                  }
 
                  if ($option_value['price'] > 0) {
                    $test .= $option_value['price_prefix'] . ' : ' . $option_value['price'];
                  }
 
                  $test .='</option>';
                }
 
                $test .= '</select>';
                $test .= '</div>';
              }
            }
          }
 
          $products_options_content_display .= $test;
 
         echo $products_options_content_display;