Skip to content

Commit 165ce49

Browse files
authored
Auto select configurable and product option setting (rapidez#515)
1 parent 544b4c4 commit 165ce49

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

config/rapidez/frontend.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,10 @@
8585
'notification' => 'z-20',
8686
'slideover' => 'z-50',
8787
],
88+
89+
// Add to cart settings to automaticly select configurable- or product options (true/false)
90+
'add_to_cart' => [
91+
'auto_select_configurable_options' => false,
92+
'auto_select_product_options' => false,
93+
],
8894
];

resources/js/components/Product/AddToCart.vue

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ export default {
4848
mounted() {
4949
this.qty = this.defaultQty
5050
this.calculatePrices()
51+
52+
this.$nextTick(() => {
53+
this.setDefaultOptions()
54+
this.setDefaultCustomSelectedOptions()
55+
})
5156
},
5257
5358
render() {
@@ -214,6 +219,35 @@ export default {
214219
215220
return addition
216221
},
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+
}
217251
},
218252
219253
computed: {
@@ -332,6 +366,20 @@ export default {
332366
333367
return disabledOptions
334368
},
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+
}
335383
},
336384
337385
watch: {

0 commit comments

Comments
 (0)