$(function(){ if(typeof ga == 'function') { var obj = JSON.parse($("#GA_Data").text()); for(var key in obj) { ga('ec:addProduct',obj[key]); console.log(obj[key]) } ga('ec:setAction','checkout',{ 'step':1 }); ga('send','event','購物車','進入購物車'); } $('.owl-carousel').owlCarousel({ loop:true, margin:0, nav:true, responsive:{ 0:{ items:1 } } }); check_max(parseInt($(".action").attr('price'))); $(".Donate_Item").on('click',function(){ $(".Donate_Item").removeClass('action'); $(this).addClass('action'); if($(this).attr('type') == 'number'){ check_max(parseInt($(this).val())); }else{ check_max(parseInt($(this).attr('price'))); } }); $("#Donate_Input").on('keyup',function(){ $(".Donate_Item").removeClass('action'); $(this).addClass('action'); var price = parseInt($(this).val()), min = parseInt($(this).attr('min')); check_max(parseInt($(this).val())); //鍵入觸發 }); //檢查最小金額 $("#Donate_Input").on('blur',function(){ var price = parseInt($(this).val()), min = parseInt($(this).attr('min')); if(price < min) { alert('最低捐款金額為'+min+'元'); $(this).val(min); } check_max(parseInt($(this).val())); }); $("#Add_To_Cart").on('click',function(){ add_to_cart(); }); $("#Move_To_Cart").on('click',function(e){ add_to_cart('2'); e.preventDefault(); }); $(document).on('change','.amountBox',function(){ check_amount($(this).attr('id')) }); edit_project(); periodCheck() }); function check_Enter() { if(window.event.keyCode==13){ return false; } } function check_max(price) { price = parseInt(price); var sid = $("#Project_ID").val(); $.ajax({ url:'/ajax/ajax_get_max_gift.php', type:'POST', cache:false, async: false, data:{sid:sid,price:price}, dataType: 'json', error:function(d) { alert(d.responseText); }, success:function(d) { if(d.Allow_Gift == '1'){ $("#Gift_Box").show(); $('.gift_allow_area').show(); $("#gift_max_num").text(d.max); if(d.Gift_Kind == '2' && d.Cumulative_Gift == '1'){ //變動式且為累積贈送 $("#Gift_Max").hide(); $('#Gift_Money').show(); }else{ $("#Gift_Max").show(); $('#Gift_Money').hide(); } var Gift_Data = d.Gift_Data; var amountBox = ''; if(d.Gift_Type == '1'){ amountBox = 'span.showAmount'; }else{ amountBox = 'input.amountBox'; } $('.gift_allow_area').find(amountBox).each(function(){ $this = $(this); var group = $this.attr('group'); var gift_no = $this.attr('gift_no'); var max = Gift_Data[group]['Gifts'][gift_no]['max']; var stock = Gift_Data[group]['Gifts'][gift_no]['stock']; $this.attr('max',max); $this.attr('stock',stock); if(max > 0){ $this.prop('disabled',false); $this.parentsUntil('.gift-list', '.item').removeClass('in-disabled'); }else{ $this.prop('disabled',true); $this.parentsUntil('.gift-list', '.item').addClass('in-disabled'); } if(d.Gift_Type == '1'){ $('.gift_flag_group').hide(); $this.text(max); $('#Gift_Amount').val(max); }else{ $('.gift_flag_group').show(); } }); }else{ $("#Gift_Box").hide(); } } }); } function check_amount(input_id=''){ if(input_id){ var max = parseInt($('#'+input_id).attr('max')); var val = parseInt($('#'+input_id).val()); if(isNaN(val)){ val = 0; $('#'+input_id).val(0); } if(val > max){ $('#'+input_id).val(max); val = max; } }else{ $('.gift_allow_area').find('input.amountBox').each(function(){ var cart = parseInt($(this).data('cart')); if(cart > 0){ $(this).val(cart); $(this).data('cart',0); } }); } var amount = 0; var money = 0; $('.gift_allow_area').find('input.amountBox').each(function(){ amount += parseInt($(this).val()); money += parseInt($(this).attr('full')) * parseInt($(this).val()); }); //您已選擇數量 $('#Gift_Amount').val(amount); //您已選擇金額 $('#gift_money_num').text(money); } function add_to_cart(move) { var sid = $("#Project_ID").val(), unit = $("#Unit_Data_ID").val(); if(sid <= 0) { window.location.href = '/'; return false; } var type = $("input[name='donateType']:checked").val(); if(type != '1' && type != '2') { alert('請選擇捐款方式'); return false; } //處理金額 var final = 0, item = $(".Donate_Item.action").eq('0'), price = parseInt(item.attr('price')) || 0, min = parseInt(item.attr('min')) || 0, text = parseInt(item.val()) || 0; //按鈕金額 if(price > 0) { final = price; } //自訂金額 else if(item.attr('type') == 'number') { //低於最小金額使用最小金額 if(text < min) { final = min; item.val(min); } else { final = text; } } if(final <= 0) { alert('請選擇或輸入捐款金額'); return false; } //處理贈品 var gift_flag = '2', gift_amount = 0, gift_max_num = 0, gift_amount_data = []; if($("#Gift_Amount").val() > 0) { gift_amount = parseInt($("#Gift_Amount").val()); } //贈品資料 $('.gift_allow_area').find('input.amountBox').each(function(){ var group = $(this).attr('group'); var gift_no = $(this).attr('gift_no'); var val = $(this).val(); gift_amount_data.push(group+','+gift_no+','+val); }); if($('#gift_max_num').text() > 0){ gift_max_num = parseInt($('#gift_max_num').text()); } if($("input[name='Gift_Flag']:checked").val() == '1') { gift_flag = '1'; } else { gift_amount = 0; } var gift_type = $('#Gift_Type').val(); if(gift_type == '2' && gift_flag == '1' && gift_amount <= 0 && gift_max_num > 0) { alert('請輸入物品數量'); return false; } //捐款期間迄 var month = $('#Pay_End_Month').val(); month = (month.length == '1') ? ('0'+month) : month; var pay_end = $('#Pay_End_Year').val()+'-'+month; $.ajax({ url:'/ajax/ajax_add_to_cart.php', type:'POST', cache:false, data:{sid:sid,type:type,price:final,gift_flag:gift_flag,unit:unit,pay_end:pay_end,gift_amount_data:gift_amount_data}, dataType:'json', error:function(d) { //alert(d.responseText); }, success:function(d) { if(d.GA && typeof ga == 'function') { for(var key in d.GA) { ga('ec:addProduct',d.GA[key]); } ga('ec:setAction','add'); ga('send','event','購物車','加入購物車'); } if(d.GA4 && typeof gtag == 'function') { gtag("event","add_to_cart",{ "items":d.GA4 }); } alert(d.msg); if(d.status == 'ok' && move == '2') { if(d.GA && typeof ga == 'function') { for(var key in d.GA) { ga('ec:addProduct',d.GA[key]); } ga('ec:setAction','checkout',{ 'step':2 }); ga('send','event','購物車','我要結帳'); } window.location.href = '/'+$("#Website_Unit_ID").val()+'/shopcart-step2/'; }else{ changeCartNum(d.total_count); } } }); } function edit_project() { if($('.Price').val() != '' && $('.input-tag-group label.action').attr('price') != $('.Price').val()){ $('.input-tag-group label.action').removeClass('action'); $('.input-tag-group label').each(function(){ if($(this).attr('price') == $('.Price').val()){ $(this).addClass('action'); return false; } }); if($('.input-tag-group label.action').length == 0){ $('#Donate_Input').addClass('action'); $('#Donate_Input').val($('.Price').val()); } check_max($('.Price').val()); } if($('.Type').val() != ''){ $('input[name="donateType"][value="'+$('.Type').val()+'"]').prop('checked', true); $('input[name="donateType"]').closest('label').removeClass('in-checked'); $('input[name="donateType"]:checked').closest('label').addClass('in-checked'); } if($('#Gift_Amount').length > 0 && $('.Gift_Flag').val() != ''){ $('input[name="Gift_Flag"][value="'+$('.Gift_Flag').val()+'"]').prop('checked', true); $('input[name="Gift_Flag"]').closest('label').removeClass('in-checked').parent('.input-flex').removeClass('in-checked') $('input[name="Gift_Flag"]:checked').closest('label').addClass('in-checked').parent('.input-flex').addClass('in-checked') if($('.Gift_Flag').val() == '1') $('#Gift_Amount').val($('.Gift_Amount').val()); else $('#Gift_Amount').val(''); } check_amount() } function periodCheck() { const needPeriod = $('.donateType[value="2"]').prop('checked') if(needPeriod) { $('#periodBox').show(400) } else { $('#periodBox').hide(400) } } // ========================================================== // 捐款期數功能 // ========================================================== ;(function($) { $('.donate-form').on('click', '.donateType', function(e) { periodCheck() }) const nowTime = new Date() const defaultEnd = new Date(nowTime.getFullYear(), nowTime.getMonth() + 2) $('#donateStart').text(`${nowTime.getFullYear()}年${nowTime.getMonth() + 1}月`) $('#donateEnd').text(`${defaultEnd.getFullYear()}年${defaultEnd.getMonth() + 1}月`) $('.donate-form').on('change', '#periodBox input[type="number"]', function(e) { if($(this).val() * 1 >= $(this).attr('max') * 1) { $(this).val($(this).attr('max')) } else if($(this).val() * 1 <= $(this).attr('min') * 1) { $(this).val($(this).attr('min')) } else { const valueCorrection = Math.round($(this).val() / $(this).attr('step')) * $(this).attr('step') $(this).val(valueCorrection) } const targetPeriod = $(this).val() - 1 const endTime = new Date(nowTime.getFullYear(), nowTime.getMonth() + targetPeriod) $('#donateEnd').text(`${endTime.getFullYear()}年${endTime.getMonth() + 1}月`) }) })($) //捐款期數限制 //最少3個月,最多8年(96個月) => 包含本月因此month-1 $(document).on('change','#Pay_End_Year',function(){ var year = $(this).val(); var today = new Date(); var st_date = new Date(); var end_date = new Date(); st_date.setMonth(today.getMonth() + 2); end_date.setMonth(today.getMonth() + 95); var option_code = ''; for (var i = 1; i <= 12; i++) { //getMonth 回傳值為0~11 if(parseInt(year) == parseInt(st_date.getFullYear()) && i < (parseInt(st_date.getMonth())+1)){ continue; } if(parseInt(year) == parseInt(end_date.getFullYear()) && i > (parseInt(end_date.getMonth())+1)){ continue; } option_code += ''; } $("#Pay_End_Month").html(option_code); })