var woo = {}; (function($) { //Keys required for accessing the woocommerce api calls (Referenced in woo.getRatePlan, woo.getCoreProduct, and woo.displayProtectionPlan) var consumer_key = "ck_63c6c0c6c1a22a78c0cbfdc9f344f74ba6c42f3b"; var consumer_secret = "cs_71499bc1c4a6429ff1c6aca680a97b0cc74b2d95"; var _self = this; _self.productInfo = undefined; //Stores product information retrieved from API call _self.productVariations = undefined;//Stores product variation information from API call in woo.getRatePlan _self.cart = undefined; //Stores the cart object created in woo.displayFallDetection _self.productName = undefined; //Stores that name of the product given in woo.getRatePlan //Function to parse together the html for the product cards based on the product JSON object sent woo.createRateButtons = function(product) { //regularPrice = parseFloat(product.regular_price).toFixed(2); var card = ''; var ratePromotions = ''; var displayPrice; salePrice = parseFloat(product.sale_price).toFixed(2); ratePromotions = product.attributes[6].option.split(","); specialOffer = product.attributes[7].option.split(","); dPrice = parseFloat(product.price).toFixed(2); ratePlan = product.attributes[1].option; product_id = product.id; noidea = product.attributes[1].option; card += ' Order Now '; //"Select'; return card; }; //Function to parse together the html for the rate plan cards based on the product JSON object sent woo.createRateCard = function(product) { //regularPrice = parseFloat(product.regular_price).toFixed(2); var card = ''; var ratePromotions = ''; var displayPrice; salePrice = parseFloat(product.sale_price).toFixed(2); ratePromotions = product.attributes[6].option.split(","); specialOffer = product.attributes[7].option.split(","); dPrice = parseFloat(product.price).toFixed(2); ratePlan = product.attributes[1].option; card += '
' + '
' + '
' + '

$'+dPrice+'/mo. '+specialOffer+'

' + '

'+ratePromotions+'

' + '
' + '
' + '

'+ratePromotions+'

' + '
' + '
' + ' Select '+ '
' + '
'; return card; }; //Function that does an ajax call returning on all the variations for a specific product. woo.getRatePlan = function(callback, productId, productName) { _self.productName = productName; $.ajax({ async: false, method: "GET", url: "/wp-json/wc/v3/products/" + productId + "/variations?per_page=100&order=asc&consumer_key=" + consumer_key + "&consumer_secret=" + consumer_secret, success: function(data) { _self.productVariations = data; callback(data); } }); }; //Function that initiates the display and creation of the modal for the upsell fall detection pendents, also creates and does initial insert into cart object. woo.displayFallDetection = function(id, price, rate_plan, rateplan_sfid, product_sfid, price_mulitplier, promotion) { //Initial add to cart _self.cart = new woo.cartPackage(); _self.cart.product_sid = product_sfid; _self.cart.rateplan_sid = rateplan_sfid; _self.cart.rateplan_multiplier = price_mulitplier; _self.cart.product_price = price; _self.cart.product_rate_price = (parseFloat(price)*parseFloat(price_mulitplier)).toFixed(2); _self.cart.ratePlan = rate_plan; if(promotion !== "none"){ _self.cart.promotionId = promotion; }else{ _self.cart.promotionId = undefined; } _self.cart.productName = _self.productName; //Modal creation var modal = document.getElementById("modalProduct"); var modalContent = woo.createFallDetectionCard(rate_plan); $("#modalProduct").html(modalContent); $("body").append(''); modal.classList.add("in"); modal.style.display = "block"; }; //Function that generates the html for the Modal Fall Detection Buttons woo.createFallDetectionCard = function(rate_plan) { var product = _self.productVariations; var fd_info; for (var i = 0; i < product.length; i++) { if ( product[i].attributes[0].option === "fall" && product[i].attributes[1].option === rate_plan ) { fd_info = product[i]; break; } } fdPrice = parseFloat(fd_info.attributes[4].option).toFixed(2); var modalContent = '"; return modalContent; }; //Function that initiates the display and creation of the modal for the upsell protection plan, also adds fall detection data to cart if add to cart is selected. //Also performs that AJAX call to get the shipping information(Might be better placed somewhere else in the flow but is here for now) woo.displayProtectionPlan = function(choice, rate_plan, sfID, price, promoID) { //If Add to Cart is selected on fall detection pendent modal add FDP info to cart if (choice == 1){ _self.cart.upsells.push([ sfID, price, "Fall Detection Button", parseFloat(_self.cart.rateplan_multiplier) * parseFloat(price), promoID ]); } //AJAX call to get the shipping variations from woocommerce then parse them into the cart object(Maybe better served at different location) $.ajax({ async: false, method: "GET", url: "/wp-json/wc/v3/products/" + localStorage.getItem('ShippingInfo') + "/variations?consumer_key=" + consumer_key + "&consumer_secret=" + consumer_secret, success: function(data) { woo.parseShipping(data); } }); //creates and displays modal for the Protection Plan var modal = document.getElementById("modalProduct"); var modalContent = woo.createProtectionPlanCard(rate_plan); $("#modalProduct").html(modalContent); modal.classList.add("in"); modal.style.display = "block"; }; //Function that generate the html for the Modal Protection Plan woo.createProtectionPlanCard = function(rate_plan) { var product = _self.productVariations; var pp_info; for (var i = 0; i < product.length; i++) { if ( product[i].attributes[0].option === "protection" && product[i].attributes[1].option === rate_plan ) { pp_info = product[i]; break; } } var modalContent = '"; return modalContent; }; //Function that initiates the display and creation of the modal for the upsell protection plan, also adds fall detection data to cart if add to cart is selected. //Also performs that AJAX call to get the shipping information(Might be better placed somewhere else in the flow but is here for now) woo.displayProtectionPlanSale = function(choice, rate_plan, sfID, price, promoID) { //If Add to Cart is selected on fall detection pendent modal add FDP info to cart if (choice == 1){ _self.cart.upsells.push([ sfID, price, "Fall Detection Sale Button", parseFloat(_self.cart.rateplan_multiplier) * parseFloat(price), promoID ]); } //AJAX call to get the shipping variations from woocommerce then parse them into the cart object(Maybe better served at different location) $.ajax({ async: false, method: "GET", url: "/wp-json/wc/v3/products/" + localStorage.getItem('ShippingInfo') + "/variations?consumer_key=" + consumer_key + "&consumer_secret=" + consumer_secret, success: function(data) { woo.parseShipping(data); } }); //creates and displays modal for the Protection Plan var modal = document.getElementById("modalProduct"); var modalContent = woo.createProtectionPlanSaleCard(rate_plan); $("#modalProduct").html(modalContent); modal.classList.add("in"); modal.style.display = "block"; }; //Function that generate the html for the Modal Protection Plan woo.createProtectionPlanSaleCard = function(rate_plan) { var product = _self.productVariations; var ppsale_info; for (var i = 0; i < product.length; i++) { if ( product[i].attributes[0].option === "protection_sale" && product[i].attributes[1].option === rate_plan ) { ppsale_info = product[i]; break; } } var modalContent = '"; return modalContent; }; //Shows cart when Product is added woo.getCart = function(){ cartCard = woo.createCart(); $('#preview-order-payment').html(cartCard); } woo.getCartItemCount = function(){ var cart = JSON.parse(sessionStorage.getItem('cart')); return cart.upsells.length + cart.accessories.length + ((cart.product_sid != undefined && cart.product_sid !== '') ? 1 : 0); } function resize(){ var cartCheck = sessionStorage.getItem('cart'); var width = jQuery(window).width(); if (cartCheck) { if (width >= 768) { jQuery('#carticon').css('display', 'inline'); jQuery('.mobcart').css('display', 'none'); } else { jQuery('.mobcart').css('display', 'inline-block'); } }else { jQuery('.menu-item-29619').css('display','none'); jQuery('.mobcart').css('display','none'); } } $(document).ready(function(){ resize(); $(window).resize(resize); }); //Function that generates the html for the cart to be viewed in the Order Summary Column on the Shipping Page woo.createCart = function() { var cartInfo = JSON.parse(sessionStorage.getItem('cart')); //console.log(cartInfo.ratePlan); if (cartInfo.ratePlan == "Annually") { cartInfo.rateplan_multiplier = "12"; //console.log(cartInfo.rateplan_multiplier); } var cart = //console.log(cartInfo.accessories); '
Order Summary
' + '
' + '
' + '' + "" + '
' + ''+cartInfo.productName+'' + "
" + '
'; //Builds the rateplan portion of cart cart+= "" + '
' + '
' + cartInfo.ratePlan + "
" + '
$'+cartInfo.product_rate_price+'/'+cartInfo.rateplan_multiplier+' mo
' + "
"; //Builds the Upsells portion of cart if (isNaN(cartInfo.upsells)) { cart+="" + '
'; for(var i = 0; i" + '
$' + cartInfo.upsells[i][3] + "/" + cartInfo.rateplan_multiplier + " mo
"; } } cart+="
" + '
'; //Builds Accessories portion of cart cart+="" + '
'; for(var i = 0; i" + '
$' + cartInfo.accessories[i][2] + "
"; } cart+= '
' + '
' + '
' + '
' + "
"; //Builds the Total portion of cart cart+="" + '
' + '
' + '
Total' + "
" + "
" + '
$'+cartInfo.totalPrice+'
' + '
' + "
" + "" + ''; var cookies = document.cookie; var arrayCookies = cookies.split(';'); var sessPromo = ''; var promoLabel = ''; //console.log(cookies); //console.log(arrayCookies); for(k in arrayCookies){ //console.log(arrayCookies[k]); if(arrayCookies[k].includes("SESSpromotion")){ //console.log("Promo found"); var arraySessPromo = arrayCookies[k].split('='); sessPromo = arraySessPromo[1]; //str.replace(/%20/g, " "); sessPromo = sessPromo.replace(/%20/g, " ");; sessPromo = sessPromo.replace(/%24/g, "$");; promoLabel = "Promotion"; break; } } cart+= "" + '
' + '
' + '
' + '
'+promoLabel+'' + "
" + "
" + "
" + '
' + '
'+sessPromo+'
' + '
' + "
" + "
"; return cart; }; //Function that gets the object cart out of session storage woo.packageObject = function() { var payLoad = sessionStorage.getItem('cart'); return payLoad; }; //Function that sends the all the information in cart to the dataPassing.php file to be parsed and pushed into Session variables in the server. woo.postProductInformation = function() { // $.ajax({ // data: "Package=" + woo.packageObject(), // //url: "/wp-content/plugins/iper-medical/templates/dataPassing.php", // url: "/wp-content/plugins/iper-medical/templates/template-product.php", // async: false, // method: "POST", // or GET // success: function() { // console.log("This was sent with a 200 code"); // alert('success'); // }, // error: function(x, msg, ex) { // alert('fail'); // console.log(msg); // } // }); $('[name="Package"]').val(woo.packageObject); $('#dataPassing').submit(); }; //Function that performs the AJAX GET call to the woocommerce API for all products woo.getCoreProducts = function(callback, $) { $.ajax({ async: false, method: "GET", url: "/wp-json/wc/v3/products?order=asc&consumer_key=" + consumer_key + "&consumer_secret=" + consumer_secret, success: function(data) { _self.productInfo = data; callback(data); } }); }; //Function that does the final add to cart for Accessories and Protection Plan choice before sending information over to Server, then navigating page to the // shipping page. woo.navShipping = function(choice, sfid, price,promoID, displayFeatimage) { //If add to cart was chosen for protection plan push information into cart var product = _self.productVariations; var ratePlan = _self.cart.ratePlan; var accessories = _self.cart.accessories; for(var i = 0; i