diff --git a/assets/js/theme/common/product-details.js b/assets/js/theme/common/product-details.js index 15382dfd53..3ffb720cc2 100644 --- a/assets/js/theme/common/product-details.js +++ b/assets/js/theme/common/product-details.js @@ -7,6 +7,7 @@ import modalFactory, { showAlertModal, modalTypes } from '../global/modal'; import { isEmpty, isPlainObject } from 'lodash'; import { normalizeFormData } from './utils/api'; import { isBrowserIE, convertIntoArray } from './utils/ie-helpers'; +import bannerUtils from "./utils/banner-utils"; export default class ProductDetails extends ProductDetailsBase { constructor($scope, context, productAttributesData = {}) { @@ -40,6 +41,7 @@ export default class ProductDetails extends ProductDetailsBase { utils.api.productAttributes.optionChange($productId, $form.serialize(), 'products/bulk-discount-rates', optionChangeCallback); } else { this.updateProductAttributes(productAttributesData); + bannerUtils.dispatchProductBannerEvent(productAttributesData); } $productOptionsElement.show(); @@ -181,6 +183,7 @@ export default class ProductDetails extends ProductDetailsBase { const productAttributesContent = response.content || {}; this.updateProductAttributes(productAttributesData); this.updateView(productAttributesData, productAttributesContent); + bannerUtils.dispatchProductBannerEvent(response.data); }); } diff --git a/assets/js/theme/common/utils/banner-utils b/assets/js/theme/common/utils/banner-utils new file mode 100644 index 0000000000..941c2f2245 --- /dev/null +++ b/assets/js/theme/common/utils/banner-utils @@ -0,0 +1,25 @@ +const bannerUtils = { + dispatchProductBannerEvent: (productAttributes) => { + let price = 0; + + if(!productAttributes.price) return; + + if (productAttributes.price.without_tax && !productAttributes.price.price_range) { + price = productAttributes.price.without_tax.value + } + + if (productAttributes.price.with_tax && !productAttributes.price.price_range) { + price = productAttributes.price.with_tax.value + } + + const evt = new CustomEvent('productpricechanged', { + detail: { + amount: price + } + }); + + window.dispatchEvent(evt); + }, +}; + +export default bannerUtils;