JavaScript - como tener varias opciones en un producto

 
Vista:

como tener varias opciones en un producto

Publicado por charly (1 intervención) el 27/11/2012 11:53:50
Espero me puedan ayudar, estoy desesperado, no se como resolver mi siguiente problema, estoy creando un carrito de compras y necesito que cada articulo tenga 3 opciones, el problema es que tengo la primera opción echa y la segunda ya no la se hacer.
No tengo casi conocimientos de programación, utilizo un programa que compre y solo tienes una opción, consigo agregar una segunda opcion pero no consigo que luego me la deje en el carro de la compra ni que me la actualice.
Aqui les dejo el codigo para ver si me pueden ayudar.

Nota.: No puedo pegar todo el codigo aqui, si alguien me puede ayudar y quiere que le envie el codigo entero, que se ponga en contacto conmigo para poder enviarle todo el codigo.

Gracias y un saludo a toda la comunidad


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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//actualizar las opciones
 _updateOption: function (id, old_option, new_option, quantity, obj) {
      x5engine.imCart.addProduct(id, 0, old_option, true, false);
      x5engine.imCart.addProduct(id, quantity, new_option);
      x5engine.imCart.showCart(_jq(obj).attr("id"), 1);
   },
 
 cartPage: function () {
      var category = x5engine.utils.getParam("category");
      if (category != null && category != "") x5engine.imCart.showCategory(x5engine.utils.getParam("category"));
      else x5engine.imCart.showCart();
   },
 
   // Save the current page and go to the cart
   gotoCart: function (in_cart) {
      if (typeof in_cart == "string") {
         _jq.imCookie("imShopPage", window.location.href, {
            path: '/'
         });
         location.href = in_cart + "cart/index.html";
         return true;
      } else {
         if (in_cart == null) in_cart = false;
         if (!in_cart) {
            _jq.imCookie("imShopPage", window.location.href, {
               path: '/'
            });
            location.href = "cart/index.html";
         } else {
            location.href = "index.html";
         }
      }
 
      return false;
   },
 
   gotoCategory: function (id) {
      _jq.imCookie("imShopPage", window.location.href);
      location.href = "cart/index.html?category=" + id;
   },
 
   // Continue shopping button
   continueShopping: function () {
      var oldPage = _jq.imCookie("imShopPage", null, {
         path: '/'
      });
      if (oldPage != null) {
         window.location.href = oldPage;
      } else window.location.href = x5engine.imCart.settings.continue_shopping_page;
   },
 
   // Add a product quantity to the cart
   // If force is true, quantity overrides the actual quantity in the cart. If force is false the quantity is added to the existing one
   addProduct: function (id, quantity, option, force, message) {
      if (message === null || message === undefined) message = true;
      if (quantity === null || quantity === undefined) quantity = 1;
      if (force === null || force === undefined) force = false;
      if (option === null || option === undefined) option = "null";
 
      if (x5engine.imCart.products[id].min_quantity !== null && x5engine.imCart.products[id].min_quantity !== undefined && quantity < x5engine.imCart.products[id].min_quantity && quantity !== 0) {
         alert((x5engine.l10n.getLocalization("cart_err_quantity")).replace("[QUANTITY]", x5engine.imCart.products[id].min_quantity));
         return false;
      }
 
      var products = x5engine.imCart._getCart();
      if ((products === null || products === undefined) && quantity > 0) {
         x5engine.imCart._setCart([{
            "id": id,
            "quantity": quantity,
            "option": escape(option)
         }]);
      } else {
         var found = false;
         for (var i = 0; i < products.length; i++) {
            if (products[i].id == id && unescape(products[i].option) == unescape(option)) {
               found = true;
               if (!force) products[i].quantity = products[i].quantity * 1 + quantity * 1;
               else {
                  if (quantity > 0) products[i].quantity = quantity * 1;
                  else if (message && confirm(x5engine.l10n.getLocalization("cart_remove_q"))) products[i] = null;
                  else if (!message) products[i] = null;
               }
            }
         }
 
         if (!found && quantity > 0) products[products.length] = {
            "id": id,
            "quantity": quantity,
            "option": escape(option)
         };
 
         x5engine.imCart._setCart(products);
      }
   },
 
   // Add a product to the cart
   addToCart: function (id, quantity, in_cart, option) {
      var product = x5engine.imCart.products[id];
      if (quantity == null) quantity = _jq("#" + x5engine.imCart.costants.QUANT_FIELD_NAME.replace("{id}", id)).val();
      if (quantity == null) quantity = 1;
      if (option == null) {
         if (_jq("#" + x5engine.imCart.costants.OPT_FIELD_NAME.replace("{id}", id)).length > 0) option = _jq("#" + x5engine.imCart.costants.OPT_FIELD_NAME.replace("{id}", id)).val();
         else option = "null";
      }
 
      if (in_cart == null) in_cart = false;
 
      if (/^[0-9]+$/.test(quantity) == false) {
         alert(x5engine.l10n.getLocalization("cart_err_qty"));
         return false;
      }
 
      x5engine.imCart.addProduct(id, quantity, option);
      x5engine.imCart.updateWidget();
      x5engine.imCart.gotoCart(in_cart);
   },
 
   // Remove a product from the cart
   removeFromCart: function (id, option, obj) {
      x5engine.imCart.addProduct(id, 0, option, true);
      x5engine.imCart.updateWidget();
      x5engine.imCart.showCart(obj, 1);
   },
 
   // Update the quantity of a product in the cart
   updateCart: function (id, obj, updateScreen, option) {
      if (updateScreen == null) updateScreen = false;
      var quantity = _jq(obj).val();
      if (/^[0-9]+$/.test(quantity) == false) {
         alert(x5engine.l10n.getLocalization("cart_err_qty"));
         return false;
      }
      var products = x5engine.imCart._getCart();
      if ( !option )
         option = "null";
      else
         option = $("#" + option).val();
      var total_quantity = 0;
      var total_weight = 0;
 
      for (var i = 0; i < products.length; i++) {
         if (products[i].id == id && (products[i].option == "null" || unescape(products[i].option) == unescape(option))) {
            option = products[i].option;
            total_weight += x5engine.imCart.products[id].weight * quantity;
            total_quantity += quantity;
         } else {
            total_weight += x5engine.imCart.products[id].weight * products[i].quantity;
            total_quantity += products[i].quantity;
         }
      }
 
      x5engine.imCart.addProduct(id, quantity, option, true);
      x5engine.imCart.updateWidget();
      if (updateScreen)
         x5engine.imCart.showCart(null, 0);
 
      x5engine.imCart.updateTotalPrice(total_quantity, total_weight);
   },
 
 
 // Show a category
   showCategory: function (id, obj) {
      obj = obj || "#imCartContainer";
      _jq(obj).empty().prepend("<h2 id=\"imPgTitle\" class=\"imTitleMargin\">" + x5engine.l10n.getLocalization('cart_step1') + "</h2>\n<div style=\"height: 15px;\">&nbsp;</div>" + x5engine.l10n.getLocalization('cart_step1_descr') + "<br /><br /><br />");
      var html = "<table id=\"imCartProductsTable\"><tr class=\"imCartHeader\">";
      html += "<td>" + x5engine.l10n.getLocalization("cart_descr") + "</td>";
      html += "<td>" + x5engine.l10n.getLocalization("cart_opt") + "</td>";
	   html += "<td>" + x5engine.l10n.getLocalization("cart_opt1") + "</td>";
      html += "<td>" + x5engine.l10n.getLocalization("cart_price") + "</td>";
      if (!x5engine.imCart.settings.vatincluded) html += "<td>" + x5engine.l10n.getLocalization("cart_vat") + "</td>";
      html += "<td>" + x5engine.l10n.getLocalization("cart_qty") + "</td>";
      html += "</tr>";
      var str_eval = "";
      for (var pid in x5engine.imCart.products) {
         var product = x5engine.imCart.products[pid];
         if (product.category == id) {
            html += "<tr><td><b>" + product.name + "</b><br />" + product.description + "</td>";
            html += "<td>";
            if (product.options != null && product.options.length > 0) {
               html += "<select id=\"product_" + pid + "_opt\">";
               for (var option in product.options)
               html += "<option value=\"" + product.options[option] + "\">" + x5engine.imCart._restoreSpecialChars(unescape(product.options[option])) + "</option>";
               html += "</select>";
            }
            html += "</td>";
 
			//option1
 
			 html += "<td>";
            if (product.options1 != null && product.options1.length > 0) {
               html += "<select id=\"product_" + pid + "_opt1\">";
               for (var option1 in product.options1)
               html += "<option value=\"" + product.options1[option] + "\">" + x5engine.imCart._restoreSpecialChars(unescape(product.options1[option])) + "</option>";
               html += "</select>";
            }
            html += "</td>";
 
 // Products
            html += "<table id=\"imCartProductsTable\"><tr class=\"imCartHeader\">";
            html += "<td>" + x5engine.l10n.getLocalization("cart_descr") + "</td>";
            html += "<td>" + x5engine.l10n.getLocalization("cart_opt") + "</td>";
			html += "<td>" + x5engine.l10n.getLocalization("cart_opt1") + "</td>";
            html += "<td>" + x5engine.l10n.getLocalization("cart_price") + "</td>";
            html += "<td>" + x5engine.l10n.getLocalization("cart_qty") + "</td>";
            html += "<td>" + x5engine.l10n.getLocalization("cart_subtot") + "</td>";
            if (!x5engine.imCart.settings.vatincluded) html += "<td>" + x5engine.l10n.getLocalization("cart_vat") + "</td>";
            html += "<td></td></tr>";
            var total = 0;
            var total_vat = 0;
            var grand_vat = 0;
            var shipping_total = 0;
            var shipping_total_vat = 0;
            var total_quantity = 0;
            var total_weight = 0;
 
            for (var i = 0; i < products.length; i++) {
               var id = products[i].id;
               var quantity = products[i].quantity;
               var option = products[i].option;
			   var option1 = products[i].option;
               var discounts = x5engine.imCart.products[id].discounts;
               var weight = x5engine.imCart.products[id].weight;
               if (weight == null) weight = 0;
               var discount = 0;
               var subtot = 0;
               var vat;
 
               if (x5engine.imCart.products[id].vat != null) vat = x5engine.imCart.products[id].vat;
               else if (x5engine.imCart.settings.vat != null) vat = x5engine.imCart.settings.vat;
               else vat = 0;
 
               if (discounts != null) discount = x5engine.imCart._getValueFromKey(quantity, discounts);
 
               total_weight += weight * quantity;
               total_quantity += quantity;
               subtot = quantity * x5engine.imCart.products[id].price;
               subtot -= (subtot * discount);
 
               total += subtot;
               grand_vat += subtot * vat;
               total_vat += subtot + subtot * vat;
 
               var cart_p = x5engine.imCart.products[id];
               email_data[id + "_" + option] = {
                  id_user: cart_p.id_user,
                  name: cart_p.name,
                  description: cart_p.description,
                  single_price: x5engine.imCart._setCurrency(x5engine.imCart.products[id].price - x5engine.imCart.products[id].price * discount),
                  price: x5engine.imCart._setCurrency(subtot),
                  price_vat: x5engine.imCart._setCurrency(subtot + subtot * vat),
                  clean_vat: subtot * vat,
                  option: option,
                  quantity: quantity,
                  vat: vat,
                  vat_f: x5engine.imCart._setCurrency(vat * (x5engine.imCart.products[id].price - x5engine.imCart.products[id].price * discount))
               };
 
               html += "<tr><td>" + x5engine.imCart.products[id].id_user + (x5engine.imCart.products[id].description != "" ? " - " + x5engine.imCart.products[id].description : "") + "</td>";
               html += "<td>";
               var freeid = "product_" + id + "_" + x5engine.utils.imHash(option) + "_opt";
               if (option != "null") {
                  html += "<select id=\"" + freeid + "\">";
                  for (var j = 0; j < x5engine.imCart.products[id].options.length; j++) {
                     html += "<option value=\"" + x5engine.imCart.products[id].options[j] + "\" " + ((x5engine.imCart.products[id].options[j] == unescape(option)) ? "selected" : "") + ">" + x5engine.imCart._restoreSpecialChars(unescape(x5engine.imCart.products[id].options[j])) + "</option>";
                     str_eval += "_jq('#" + freeid + "').unbind('change').change(function () {x5engine.imCart._updateOption('" + id + "', '" + option + "', _jq('#" + freeid + "').val() , " + quantity + ", '" + _jq(obj).attr("id") + "')});";
                  }
                  html += "</select>";
               }
               html += "</td>";
 
			   // option1
 
			  html += "<td>";
 
               var angulo = "product_" + id + "_" + x5engine.utils.imHash(option) + "_opt1";
               if (option1 != "null") {
                  html += "<select id=\"" + angulo + "\">";
                  for (var j = 0; j < x5engine.imCart.products[id].options1.length; j++) {
                     html += "<option value=\"" + x5engine.imCart.products[id].options1[j] + "\" " + ((x5engine.imCart.products[id].options1[j] == unescape(option)) ? "selected" : "") + ">" + x5engine.imCart._restoreSpecialChars(unescape(x5engine.imCart.products[id].options1[j])) + "</option>";
                     str_eval += "_jq('#" + angulo + "').unbind('change').change(function () {x5engine.imCart._updateOption('" + id + "', '" + option1 + "', _jq('#" + angulo + "').val() , " + quantity + ", '" + _jq(obj).attr("id") + "')});";
                  }
                  html += "</select>";
               }
               html += "</td>";
 
// Aqui recojo las variables
 
for (var product in data.products) {
               html += "<tr valign=\"top\"><td>" + data.products[product].name + ((data.products[product].option != "null") ? " - " + x5engine.imCart._restoreSpecialChars(unescape(data.products[product].option)) : "") + ((data.products[product].option1 != "null") ? " - " + x5engine.imCart._restoreSpecialChars(unescape(data.products[product].option1)) : "") + (data.products[product].description != "" ? "<p>" + data.products[product].description + "</p>" : "") + "</td>";
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder