|
| 1 | +import Vue from 'vue' |
| 2 | +import EventBus from '@vue-storefront/core/compatibility/plugins/event-bus' |
| 3 | +import omit from 'lodash-es/omit' |
| 4 | +import i18n from '@vue-storefront/i18n' |
| 5 | +import { Logger } from '@vue-storefront/core/lib/logger' |
| 6 | +import { optionLabel } from './optionLabel' |
| 7 | +import { setProductConfigurableOptions } from './productOptions' |
| 8 | +import config from 'config' |
| 9 | +import { findConfigurableVariant } from './variant' |
| 10 | +import { hasImage } from './' |
| 11 | + |
| 12 | +export function populateProductConfigurationAsync (context, { product, selectedVariant }) { |
| 13 | + Logger.warn('deprecated, will be not used from 1.12')() |
| 14 | + if (product.configurable_options) { |
| 15 | + for (let option of product.configurable_options) { |
| 16 | + let attribute_code |
| 17 | + let attribute_label |
| 18 | + if (option.attribute_code) { |
| 19 | + attribute_code = option.attribute_code |
| 20 | + attribute_label = option.label ? option.label : (option.frontend_label ? option.frontend_label : option.default_frontend_label) |
| 21 | + } else { |
| 22 | + if (option.attribute_id) { |
| 23 | + let attr = context.rootState.attribute.list_by_id[option.attribute_id] |
| 24 | + if (!attr) { |
| 25 | + Logger.error('Wrong attribute given in configurable_options - can not find by attribute_id', option)() |
| 26 | + continue |
| 27 | + } else { |
| 28 | + attribute_code = attr.attribute_code |
| 29 | + attribute_label = attr.frontend_label ? attr.frontend_label : attr.default_frontend_label |
| 30 | + } |
| 31 | + } else { |
| 32 | + Logger.error('Wrong attribute given in configurable_options - no attribute_code / attribute_id', option)() |
| 33 | + } |
| 34 | + } |
| 35 | + let selectedOption = null |
| 36 | + if (selectedVariant.custom_attributes) { |
| 37 | + selectedOption = selectedVariant.custom_attributes.find((a) => { // this is without the "label" |
| 38 | + return (a.attribute_code === attribute_code) |
| 39 | + }) |
| 40 | + } else { |
| 41 | + selectedOption = { |
| 42 | + attribute_code: attribute_code, |
| 43 | + value: selectedVariant[attribute_code] |
| 44 | + } |
| 45 | + } |
| 46 | + if (option.values && option.values.length) { |
| 47 | + const selectedOptionMeta = option.values.find(ov => { return ov.value_index === selectedOption.value }) |
| 48 | + if (selectedOptionMeta) { |
| 49 | + selectedOption.label = selectedOptionMeta.label ? selectedOptionMeta.label : selectedOptionMeta.default_label |
| 50 | + selectedOption.value_data = selectedOptionMeta.value_data |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + const confVal = { |
| 55 | + attribute_code: attribute_code, |
| 56 | + id: selectedOption.value, |
| 57 | + label: selectedOption.label ? selectedOption.label : /* if not set - find by attribute */optionLabel(context.rootState.attribute, { attributeKey: selectedOption.attribute_code, searchBy: 'code', optionId: selectedOption.value }) |
| 58 | + } |
| 59 | + Vue.set(context.state.current_configuration, attribute_code, confVal) |
| 60 | + } |
| 61 | + setProductConfigurableOptions({ |
| 62 | + product, |
| 63 | + configuration: context.state.current_configuration, |
| 64 | + setConfigurableProductOptions: config.cart.setConfigurableProductOptions |
| 65 | + }) // set the custom options |
| 66 | + } |
| 67 | + return selectedVariant |
| 68 | +} |
| 69 | + |
| 70 | +export function configureProductAsync (context, { product, configuration, selectDefaultVariant = true, fallbackToDefaultWhenNoAvailable = true, setProductErorrs = false }) { |
| 71 | + Logger.warn('deprecated, will be not used from 1.12')() |
| 72 | + // use current product if product wasn't passed |
| 73 | + if (product === null) product = context.getters.getCurrentProduct |
| 74 | + const hasConfigurableChildren = (product.configurable_children && product.configurable_children.length > 0) |
| 75 | + |
| 76 | + if (hasConfigurableChildren) { |
| 77 | + // handle custom_attributes for easier comparing in the future |
| 78 | + product.configurable_children.forEach((child) => { |
| 79 | + let customAttributesAsObject = {} |
| 80 | + if (child.custom_attributes) { |
| 81 | + child.custom_attributes.forEach((attr) => { |
| 82 | + customAttributesAsObject[attr.attribute_code] = attr.value |
| 83 | + }) |
| 84 | + // add values from custom_attributes in a different form |
| 85 | + Object.assign(child, customAttributesAsObject) |
| 86 | + } |
| 87 | + }) |
| 88 | + // find selected variant |
| 89 | + let desiredProductFound = false |
| 90 | + let selectedVariant = findConfigurableVariant({ product, configuration, availabilityCheck: true }) |
| 91 | + if (!selectedVariant) { |
| 92 | + if (fallbackToDefaultWhenNoAvailable) { |
| 93 | + selectedVariant = findConfigurableVariant({ product, selectDefaultChildren: true, availabilityCheck: true }) // return first available child |
| 94 | + desiredProductFound = false |
| 95 | + } else { |
| 96 | + desiredProductFound = false |
| 97 | + } |
| 98 | + } else { |
| 99 | + desiredProductFound = true |
| 100 | + } |
| 101 | + |
| 102 | + if (selectedVariant) { |
| 103 | + if (!desiredProductFound && selectDefaultVariant /** don't change the state when no selectDefaultVariant is set */) { // update the configuration |
| 104 | + populateProductConfigurationAsync(context, { product: product, selectedVariant: selectedVariant }) |
| 105 | + configuration = context.state.current_configuration |
| 106 | + } |
| 107 | + if (setProductErorrs) { |
| 108 | + product.errors = {} // clear the product errors |
| 109 | + } |
| 110 | + product.is_configured = true |
| 111 | + |
| 112 | + if (config.cart.setConfigurableProductOptions && !selectDefaultVariant && !(Object.keys(configuration).length === 1 && configuration.sku)) { |
| 113 | + // the condition above: if selectDefaultVariant - then "setCurrent" is seeting the configurable options; if configuration = { sku: '' } -> this is a special case when not configuring the product but just searching by sku |
| 114 | + setProductConfigurableOptions({ |
| 115 | + product, |
| 116 | + configuration, |
| 117 | + setConfigurableProductOptions: config.cart.setConfigurableProductOptions |
| 118 | + }) // set the custom options |
| 119 | + }/* else { |
| 120 | + Logger.debug('Skipping configurable options setup', configuration)() |
| 121 | + } */ |
| 122 | + const fieldsToOmit = ['name'] |
| 123 | + if (!hasImage(selectedVariant)) fieldsToOmit.push('image') |
| 124 | + selectedVariant = omit(selectedVariant, fieldsToOmit) // We need to send the parent SKU to the Magento cart sync but use the child SKU internally in this case |
| 125 | + // use chosen variant for the current product |
| 126 | + if (selectDefaultVariant) { |
| 127 | + context.dispatch('setCurrent', selectedVariant) |
| 128 | + } |
| 129 | + EventBus.$emit('product-after-configure', { product: product, configuration: configuration, selectedVariant: selectedVariant }) |
| 130 | + } |
| 131 | + if (!selectedVariant && setProductErorrs) { // can not find variant anyway, even the default one |
| 132 | + product.errors.variants = i18n.t('No available product variants') |
| 133 | + if (selectDefaultVariant) { |
| 134 | + context.dispatch('setCurrent', product) // without the configuration |
| 135 | + } |
| 136 | + } |
| 137 | + return selectedVariant |
| 138 | + } else { |
| 139 | + if (fallbackToDefaultWhenNoAvailable) { |
| 140 | + return product |
| 141 | + } else { |
| 142 | + return null |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments