let OVERSEAS_SHIPPING = false, COUNTRY = [], PROVINCE = [], CITY = [], SUBDISTRICT = [], CART_ITEMS = [], COURIER = [], ORIGIN = 455, TOTAL_WEIGHT = 0, TOTAL_AMOUNT = 0 $(document).ready(function(){ stepper = new Stepper($('.bs-stepper')[0]) getCartDetail() getCountry() getProvince() $('#is_overseas_hipping').change(function() { if(this.checked) { OVERSEAS_SHIPPING = true $('#country-option').css("display", "block") $('#province-option').css("display", "none") $('#city-option').css("display", "none") $('#subdistrict-option').css("display", "none") $('#city').prop("disabled", true); $('#subdistrict').prop("disabled", true); renderCountry() }else{ OVERSEAS_SHIPPING = false $('#country-option').css("display", "none") $('#province-option').css("display", "block") $('#city-option').css("display", "block") $('#subdistrict-option').css("display", "block") renderProvince() } }) //watch province option $('#province').change(function() { $('#city').prop("disabled", true); $('#subdistrict').prop("disabled", true); $('#city').prop('selectedIndex',0); $('#subdistrict').prop('selectedIndex',0); const selectedProvince = $('#province').find(":selected").val() console.log(selectedProvince) if (parseInt(selectedProvince) > 0) { getCity(selectedProvince) } else { $('#city').html(``); $('#city').prop("disabled", true); } }) //watch city option $('#city').change(function() { $('#subdistrict').prop("disabled", true); $('#subdistrict').prop('selectedIndex',0); const selectedCity = $('#city').find(":selected").val() console.log(selectedCity) if (parseInt(selectedCity) > 0) { getSubdistrict(selectedCity) } else { $('#subdistrict').html(``); $('#subdistrict').prop("disabled", true); } }) //watch county option $('#subdistrict').change(function() { const selectedSubdistrict = $('#subdistrict').find(":selected").val() console.log(selectedSubdistrict) }) $('#address-button').click(function(){ submitAddressForm() }) $('#ship-button').click(function(){ submitShippingForm() }) $('.address-update-button').click(function(){ stepper.to(1) $('#address-button').css("display", "block") $('#ship-button').css("display", "none") $('#pay-button').css("display", "none") }) $('.shipping-update-button').click(function(){ stepper.to(2) $('#address-button').css("display", "none") $('#ship-button').css("display", "block") $('#pay-button').css("display", "none") }) $('#pay-button').click(function(){ checkoutCart() }) }) function submitAddressForm(){ let $form = $('#address-form'), receiverName = $form.find( "input[name='receiver_name']" ).val(), fullAddress = $form.find( "textarea[name='full_address']" ).val(), postalCode = $form.find( "input[name='postal_code']" ).val(), phone = $form.find( "input[name='phone']" ).val(), country = $('#country-option').find(":selected").val(), province = $('#province-option').find(":selected").val(), city = $('#city-option').find(":selected").val(), subdistrict = $('#subdistrict-option').find(":selected").val() let fields = [receiverName, fullAddress, postalCode, phone] if(OVERSEAS_SHIPPING) fields.push(country) else fields.push(province, city, subdistrict) if(checkForm(fields)){ stepper.next() renderAddressBox() getCourier() $('#ship-button').css("display", "block") $('#address-button').css("display", "none") }else{ showError("Alamat", "Form Alamat Harus diisi") } } function submitShippingForm(){ let $form = $('#shipping-form'), shippingService = $form.find('input[name="shipping_method"]:checked').val(); console.log("SHIPPING SERVICE: ", shippingService) let fields = [shippingService] if(checkForm(fields)){ stepper.next() renderShippingBox() $('#ship-button').css("display", "none") $('#pay-button').css("display", "block") }else{ showError("Metode Pengiriman", "Anda harus memilih metode pengiriman") } } function checkoutCart(){ startLoadingButton('#pay-button') let $addressForm = $('#address-form'), $shippingForm = $('#shipping-form'), selectedCourier = $shippingForm.find('input[name="shipping_method"]:checked').val(); arr = selectedCourier.split("|"), service = arr[0], serviceType = arr[1], etd = arr[2], cost = arr[3], grandTotal = parseInt(cost) + parseInt(TOTAL_AMOUNT), request = { receiver_name: $addressForm.find("input[name='receiver_name']").val(), service: service, service_type: serviceType, service_price: cost, full_address: $addressForm.find("textarea[name='full_address']").val(), postal_code: $addressForm.find("input[name='postal_code']").val(), phone: $addressForm.find("input[name='phone']").val() } if(OVERSEAS_SHIPPING){ request.country = $("#country").find(":selected").text() request.country_id = $("#country").find(":selected").val() }else{ request.province = $("#province").find(":selected").text() request.province_id = $("#province").find(":selected").val() request.city = $("#city").find(":selected").text() request.city_id = $("#city").find(":selected").val() request.subdistrict = $("#subdistrict").find(":selected").text() request.subdistrict_id = $("#subdistrict").find(":selected").val() } $.ajax({ async: true, url: `${CART_API_URL}checkout`, type: 'POST', data: JSON.stringify(request), beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', `Bearer ${SESSION}`); }, error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) endLoadingButton('#pay-button', 'Bayar') }, success: function(res) { let data = res.data endLoadingButton('#pay-button', 'Bayar') openMidtrans(data) } }); } function checkForm(fields){ let isValid = true for(let field of fields){ if(!field){ isValid = false break } } return isValid } function getCartDetail(){ $.ajax({ async: true, url: `${CART_API_URL}detail`, type: 'GET', beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', `Bearer ${SESSION}`); }, error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) }, success: function(res) { CART = res.data if(CART){ CART_ITEMS = CART.items if(CART_ITEMS.length > 0){ TOTAL_AMOUNT = CART.total_amount renderCartItems() getTotalWeight() }else{ window.location.href = `${WEB_URL}carts` } }else{ window.location.href = `${WEB_URL}carts` } } }); } function getCountry(){ $.ajax({ async: true, url: `${VENDOR_API_URL}international-destination`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) }, success: function(res) { COUNTRY = res.data.rajaongkir.results } }); } function getProvince(){ $.ajax({ async: true, url: `${VENDOR_API_URL}province`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) }, success: function(res) { PROVINCE = res.data.rajaongkir.results renderProvince() } }); } function getCity(province) { $.ajax({ async: true, url: `${VENDOR_API_URL}city?province_id=${province}`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) }, success: function(res) { CITY = res.data.rajaongkir.results renderCity() } }) } function getSubdistrict(city) { $.ajax({ async: true, url: `${VENDOR_API_URL}subdistrict?city_id=${city}`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) }, success: function(res) { SUBDISTRICT = res.data.rajaongkir.results renderSubdistrict() } }) } function getCourier() { let origin = ORIGIN, destination = OVERSEAS_SHIPPING ? $('#country').find(":selected").val() : $('#subdistrict').find(":selected").val(), weight = TOTAL_WEIGHT if(OVERSEAS_SHIPPING){ $.ajax({ async: true, url: `${VENDOR_API_URL}international-shipping-cost?origin=${origin}&destination=${destination}&weight=${weight}&courier=pos`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) }, success: function(res) { COURIER = res.data.rajaongkir.results renderCourier() } }) }else{ $.ajax({ async: true, url: `${VENDOR_API_URL}shipping-cost?origin=${origin}&origin_type=subdistrict&destination=${destination}&destination_type=subdistrict&weight=${weight}`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) }, success: function(res) { COURIER = res.data.rajaongkir.results renderCourier() } }) } } function renderCartItems(){ console.log("RENDER ITEMS") let itemsHtml = `

Keranjang

` for(let item of CART_ITEMS){ let itemImages = item.product_images let productVariant = item.product_variant let imgUrl = `${WEB_URL}assets/img/default.png` if(itemImages.length > 0){ imgUrl = itemImages[0].full_thumbnail_url } let variant = productVariant ? productVariant.variants : '' let price = CURRENCY == 'usd' ? formatDollar(item.price) : formatRupiah(item.price, true) let itemHtml = `
${item.product.title} ${variant}

${item.product.title}

${variant}

${price}

Jumlah: ${item.quantity}
` itemsHtml += itemHtml } console.log(itemsHtml) let totalAmount = CURRENCY == 'usd' ? formatDollar(TOTAL_AMOUNT) : formatRupiah(TOTAL_AMOUNT, true) let totalAmountHtml = `${totalAmount}` $('.carts-item').html(itemsHtml) $('#total-amount').html(totalAmountHtml) $('.item-lazyload').lazyload() } function renderCountry(){ let listCountryHtml = `` for (const item of COUNTRY) { const countryHtml = `` listCountryHtml += countryHtml } $('#country').html(listCountryHtml) $('#country').prop("disabled", false); } function renderProvince(){ let listProvinceHtml = `` for (const item of PROVINCE) { const provinceHtml = `` listProvinceHtml += provinceHtml } $('#province').html(listProvinceHtml) $('#province').prop("disabled", false); } function renderCity(){ let listCityHtml = `` for (const item of CITY) { const cityHtml = `` listCityHtml += cityHtml } $('#city').html(listCityHtml) $('#city').prop("disabled", false); } function renderSubdistrict(){ let listSubdistrictHtml = `` for (const item of SUBDISTRICT) { const subdistrictHtml = `` listSubdistrictHtml += subdistrictHtml } $('#subdistrict').html(listSubdistrictHtml) $('#subdistrict').prop("disabled", false); } function renderCourier(){ let listCourierHtml = '' let index = 0 for(const item of COURIER){ const courierCode = item.code const courierOption = item.costs if(courierOption.length > 0){ for(const courier of courierOption){ const cost = Array.isArray(courier.cost) ? courier.cost[0].value.toString() : courier.cost.toString() let etd = Array.isArray(courier.cost) ? courier.cost[0].etd : courier.etd if(!etd.toLowerCase().includes("hari")) etd = `${etd} Hari` let costCurr = CURRENCY == 'usd' ? formatDollar(cost) : formatRupiah(cost, true) const courierHtml = `
${courierCode.toUpperCase()}-${courier.service}
Estimasi pengiriman ${etd}
${costCurr}
` listCourierHtml += courierHtml } } index++ } $('#shipping-form').html(listCourierHtml) } function renderAddressBox(){ let $form = $('#address-form'), receiver = $form.find("input[name='receiver_name']").val(), fullAddress = $form.find("textarea[name='full_address']").val(), country = $("#country").find(":selected").text(), province = $("#province").find(":selected").text(), city = $("#city").find(":selected").text(), subdistrict = $("#subdistrict").find(":selected").text(), postalCode = $form.find("input[name='postal_code']").val(), phone = $form.find("input[name='phone']").val() $('.receiver-name').html(receiver) $('.full-address').html(fullAddress) $('.address').html(OVERSEAS_SHIPPING ? country : `${province}, ${city}, ${subdistrict}`) $('.postal-code').html(postalCode) $('.phone').html(phone) } function renderShippingBox(){ let $form = $('#shipping-form'), selectedCourier = $form.find('input[name="shipping_method"]:checked').val(); arr = selectedCourier.split("|"), service = arr[0], serviceType = arr[1], etd = arr[2], cost = arr[3], grandTotal = parseInt(cost) + parseInt(TOTAL_AMOUNT) console.log("COURIER: ", arr) let shippingCost = CURRENCY == 'usd' ? formatDollar(cost.toString()) : formatRupiah(cost.toString(), true) let shippingBoxHtml = `${service.toUpperCase()} ${serviceType} Estimasi pengiriman ${etd} - ${shippingCost}` let costCurr = CURRENCY == 'usd' ? formatDollar(cost.toString()) : formatRupiah(cost.toString(), true) let costHtml = `${costCurr}` let grandTotalCurr = CURRENCY == 'usd' ? formatDollar(grandTotal.toString()) : formatRupiah(grandTotal.toString(), true) let grandTotalHtml = `${grandTotalCurr}` $('.shipping-box').html(shippingBoxHtml) $('.shipping-price').html(costHtml) $('#grand-total').html(grandTotalHtml) } function getTotalWeight(){ for(const item of CART_ITEMS){ let product = item.product let weightAmount = parseInt(item.quantity) * parseInt(product.weight) TOTAL_WEIGHT += weightAmount } console.log("TOTAL WEIGHT: ", TOTAL_WEIGHT) } function openMidtrans(data){ snap.pay(data.midtrans_token) }