lifestyle clotheslines multi add to cart select options
// create object to hold product ids
var child_ids = new Object();
// hide product ids from select options
$('.productAttributeValue select option').each(function(){
if ($(this).text().indexOf("]") > -1) {
select_label = $(this).text().replace(/\[(.*?)\]/, '');
$(this).attr('label', select_label);
$(this).attr('value', '');
}
})
//store the main product price as a variable
var main_product_price = $(".product-details .ProductPrice").text();
var main_product_price_number = Number(main_product_price.replace(/[^0-9\.]+/g,""));
//reset select option on page load (esp. for when hitting 'back')
$( document ).ready(function() {
$('.productOptionViewSelect select').prop('selectedIndex',0);
});
$(document).on('change', 'select', function() {
$('.productOptionViewSelect select').each(function(){
var label = $(this).find(':selected').parents('.productAttributeValue').prev('.productAttributeLabel').find('span').text().trim();
var value = $(this).find(':selected').text();
value = value.substring(value.indexOf('[')+1, value.indexOf(']'));
///add price of option to total product price
if (value != "") { // if option has a value other than 'none'
var path = '%%GLOBAL_ShopPath%%/products.php?productId=' + value;
if(path != "") {
path = path.replace("%%GLOBAL_ShopPath%%","").replace("%%GLOBAL_ShopPath%%","");
var item = path;
$.ajax({
url: path,
type: "GET",
async: true,
success: processData(item)
});
}
function processData(item){
return function(data){
var option_price = jQuery(data).find(".product-details .ProductPrice").text();
var option_price_number = Number(option_price.replace(/[^0-9\.]+/g,""));
child_ids[label] = { prod_id: parseInt(value), price: option_price_number };
console.log(child_ids)
// update price funtion
var option_total_price = 0;
$.each(child_ids, function(i, rep){ option_total_price += rep.price });
$(".product-details .ProductPrice").text("$" + (main_product_price_number + option_total_price) );
}
}
}
else { //if option value is set to none
delete child_ids[label]; //remove item from object if select is set to none
var option_total_price = 0;
$.each(child_ids, function(i, rep){ option_total_price += rep.price });
$(".product-details .ProductPrice").text("$" + (main_product_price_number + option_total_price).toFixed(2) );
}
}); // end each select
});
function ajax_product_add(id) {
add_url = $('#productDetailsAddToCartForm').attr('action') + '?action=add&product_id='+id;
$.ajax({
url: add_url,
dataType: 'html',
async: false,
success: function(data) {
console.log("Added!");
console.log(add_url);
console.log(data);
},
error: function(data) {
console.log("Error!");
console.log(add_url);
console.log(data);
}
});
}
function check_add_to_cart(form, required) {
console.log("thisone");
var valid = true;
var qtyInputs = $(form).find('input.qtyInput');
qtyInputs.each(function() {
if(isNaN($(this).val()) || $(this).val() <= 0) {
alert(lang.InvalidQuantity);
this.focus();
this.select();
valid = false;
return false;
}
});
if(valid == false) {
return false;
}
if(!CheckProductConfigurableFields(form)) {
return false;
}
// validate the attributes
var attributesValidated = $('#productDetailsAddToCartForm')
.validate()
.form();
if (!attributesValidated) {
return false;
}
if (!CheckQuantityLimits(form)) {
return false;
}
if(required && !$(form).find('.CartVariationId').val()) {
alert(lang.OptionMessage);
var select = $(form).find('select').get(0);
if(select) {
select.focus();
}
var radio = $(form).find('input[type=radio]').get(0);
if(radio) {
radio.focus();
}
return false;
}
if (!CheckEventDate()) {
return false;
}
for (key in child_ids) {
ajax_product_add(child_ids[key].prod_id);
}
}