Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to automaticly select configurable- and product options #515

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/rapidez/frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@
'notification' => 'z-20',
'slideover' => 'z-50',
],

// Add to cart settings to automaticly select configurable- or product options (true/false)
'add_to_cart' => [
'auto_select_configurable_options' => false,
'auto_select_product_options' => false,
],
];
48 changes: 48 additions & 0 deletions resources/js/components/Product/AddToCart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export default {
mounted() {
this.qty = this.defaultQty
this.calculatePrices()

this.$nextTick(() => {
this.setDefaultOptions()
this.setDefaultCustomSelectedOptions()
})
},

render() {
Expand Down Expand Up @@ -214,6 +219,35 @@ export default {

return addition
},
async setDefaultOptions() {
if (!window.config.add_to_cart.auto_select_configurable_options || !window.config.product?.super_attributes) {
return
}
// We do not loop and use the values of enabledOptions directly.
// This is on purpose in order to force recalculations of enabledOptions to be considered.
Object.keys(this.enabledOptions).map((attributeKey) => {
Vue.set(this.options, attributeKey, this.enabledOptions[attributeKey].find(Boolean))
})
},
async setDefaultCustomSelectedOptions() {
if (!window.config.add_to_cart.auto_select_product_options || !window.config.product?.options) {
return
}

window.config.product.options.map((option) => {
if (!option.is_require || !option.values) {
return
}
let value = option.values
.sort((aValue, bValue) => aValue.sort_order - bValue.sort_order)
.find((value) => value.in_stock)?.option_type_id
if (!value) {
return
}

Vue.set(this.customSelectedOptions, option.option_id, value)
})
}
},

computed: {
Expand Down Expand Up @@ -332,6 +366,20 @@ export default {

return disabledOptions
},
enabledOptions: function () {
let valuesPerAttribute = {}
Object.entries(this.product.super_attributes).forEach(([attributeId, attribute]) => {
valuesPerAttribute[attributeId] = []
// Fill list with products per attribute value
Object.entries(this.product.children).forEach(([productId, product]) => {
if (!product.in_stock || this.disabledOptions['super_' + attribute.code].includes(product.value)) {
return
}
valuesPerAttribute[attributeId].push(product[attribute.code])
})
})
return valuesPerAttribute
}
},

watch: {
Expand Down
Loading