UNPKG

@wallfar/ocd-studio-core-sdk

Version:

Helper SDK for our OneClick Studio modules

30 lines (29 loc) 1 kB
import { reactive } from 'vue'; export function createProductConfigurator(product) { const selection = reactive(product.options?.reduce((acc, o) => { acc[o.name] = o.values[0]?.value || ''; return acc; }, {})); function getSelectedVariant() { if (product.variants?.length === 0) return _getMainProductAsVariant(); return getVariant(selection); } function getVariant(variant) { if (product.variants?.length === 0) return _getMainProductAsVariant(); return product.variants?.find((v) => v.options.every((o) => o.value === variant[o.option])); } function _getMainProductAsVariant() { return { options: [], combinedOptions: '', stock: product.stock, price: product.price, image: product.media?.[0], disabled: false, name: product.title }; } return { selection, getSelectedVariant, getVariant }; }