Originally posted by lesliekirk
View Post
where a product is using inventory variants and non-variant attributes
if (typeof attrMachCall === 'undefined' && $.hook('product-attribute__count').val() > 0) { var baseProductPrice = Number($.hook('product-details__price').attr('content')), regularProductPrice = Number(), productAttributeCount = Number($.hook('product-attribute__count').val() + 1), productAttributes = $.hook('cmp-product-purchase').find(':input'), attributeType = ['']; for (var i = 0; i < productAttributes.length; i++) { var tagName = productAttributes[i].tagName.toLowerCase(), elementType = productAttributes[i].type, type = productAttributes[i].getAttribute('data-attribute-type'), name = productAttributes[i].name; if (elementType === 'hidden' && type != null) { attributeType.push(type); } // Update product display price on any attribute value change productAttributes[i].onchange = function () { updateProductDisplayPrice(); }; } // Update product display price based on the total of selected options function updateProductDisplayPrice() { var availableAttributes, selectedOption, selectedOptionData, totalAdjustedProductPrice = baseProductPrice, totalRegularProductPrice = regularProductPrice, totalQuantity; for (var attributeIndex = 1; attributeIndex < productAttributeCount; attributeIndex++) { availableAttributes = document.getElementsByName('Product_Attributes[' + attributeIndex + ']:value'); if (attributeType[attributeIndex] === 'select') { for (var optionIndex = 0; optionIndex < availableAttributes.length; optionIndex++) { selectedOption = availableAttributes.item(optionIndex); selectedOptionData = selectedOption.options.item(selectedOption.selectedIndex); totalAdjustedProductPrice += Number(selectedOptionData.getAttribute('data-option-price')); totalRegularProductPrice += Number(selectedOptionData.getAttribute('data-regular-price')); } } else if (attributeType[attributeIndex] === 'radio' || attributeType[attributeIndex] === 'checkbox') { for (var radioOptionIndex = 0; radioOptionIndex < availableAttributes.length; radioOptionIndex++) { selectedOption = availableAttributes.item(radioOptionIndex); if (selectedOption.checked) { totalAdjustedProductPrice += Number(selectedOption.getAttribute('data-option-price')); totalRegularProductPrice += Number(selectedOption.getAttribute('data-regular-price')); } } } else if (attributeType[attributeIndex] === 'text' || attributeType[attributeIndex] === 'memo') { selectedOption = availableAttributes.item(0); if (selectedOption.value) { totalAdjustedProductPrice += Number(selectedOption.getAttribute('data-option-price')); totalRegularProductPrice += Number(selectedOption.getAttribute('data-regular-price')); } } } totalQuantity = document.getElementsByName('Quantity'); totalAdjustedProductPrice = totalAdjustedProductPrice * Number(totalQuantity.item(0).value); totalRegularProductPrice = totalRegularProductPrice * Number(totalQuantity.item(0).value); $.hook('product-details__price').html(formatCurrency(totalAdjustedProductPrice)); } // Format for US currency function formatCurrency(total) { var neg = false; if (total < 0) { neg = true; total = Math.abs(total); } return (neg ? '-$' : '$') + parseFloat(total, 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,').toString(); } // Update product display price based on default attributes on page load updateProductDisplayPrice(); }
Comment