@@ -48,6 +48,11 @@ export default {
48
48
mounted () {
49
49
this .qty = this .defaultQty
50
50
this .calculatePrices ()
51
+
52
+ this .$nextTick (() => {
53
+ this .setDefaultOptions ()
54
+ this .setDefaultCustomSelectedOptions ()
55
+ })
51
56
},
52
57
53
58
render () {
@@ -214,6 +219,35 @@ export default {
214
219
215
220
return addition
216
221
},
222
+ async setDefaultOptions () {
223
+ if (! window .config .add_to_cart .auto_select_configurable_options || ! window .config .product ? .super_attributes ) {
224
+ return
225
+ }
226
+ // We do not loop and use the values of enabledOptions directly.
227
+ // This is on purpose in order to force recalculations of enabledOptions to be considered.
228
+ Object .keys (this .enabledOptions ).map ((attributeKey ) => {
229
+ Vue .set (this .options , attributeKey, this .enabledOptions [attributeKey].find (Boolean ))
230
+ })
231
+ },
232
+ async setDefaultCustomSelectedOptions () {
233
+ if (! window .config .add_to_cart .auto_select_product_options || ! window .config .product ? .options ) {
234
+ return
235
+ }
236
+
237
+ window .config .product .options .map ((option ) => {
238
+ if (! option .is_require || ! option .values ) {
239
+ return
240
+ }
241
+ let value = option .values
242
+ .sort ((aValue , bValue ) => aValue .sort_order - bValue .sort_order )
243
+ .find ((value ) => value .in_stock )? .option_type_id
244
+ if (! value) {
245
+ return
246
+ }
247
+
248
+ Vue .set (this .customSelectedOptions , option .option_id , value)
249
+ })
250
+ }
217
251
},
218
252
219
253
computed: {
@@ -332,6 +366,20 @@ export default {
332
366
333
367
return disabledOptions
334
368
},
369
+ enabledOptions : function () {
370
+ let valuesPerAttribute = {}
371
+ Object .entries (this .product .super_attributes ).forEach (([attributeId , attribute ]) => {
372
+ valuesPerAttribute[attributeId] = []
373
+ // Fill list with products per attribute value
374
+ Object .entries (this .product .children ).forEach (([productId , product ]) => {
375
+ if (! product .in_stock || this .disabledOptions [' super_' + attribute .code ].includes (product .value )) {
376
+ return
377
+ }
378
+ valuesPerAttribute[attributeId].push (product[attribute .code ])
379
+ })
380
+ })
381
+ return valuesPerAttribute
382
+ }
335
383
},
336
384
337
385
watch: {
0 commit comments