(function () { if (document.getElementById("reforestation-app-load") !== null) { return; } document.querySelector("body").insertAdjacentHTML('beforeend', ''); var hash = 'cqXTtsnM7l'; var sessionId = '5c06c53b52aab3fd400613dd24a366d2'; var variantId = ''; var checkout = window.Shopify && window.Shopify.checkout; var oneTreePlanted = function (jQuery) { jQuery('body').prepend(''); jQuery('[data-selector="one-tree-planted-tree-counter"]').html('0'); var hasProductInCart = false; if (typeof checkout !== "undefined") { checkout['line_items'].forEach(lineItem => { if(lineItem.variant_id == variantId || lineItem.title === 'Tree to be Planted' || lineItem.vendor === 'One Tree Planted' || lineItem.title === 'Donation of a Tree' || lineItem.vendor === 'Donation of a Tree') { hasProductInCart = true; } }); if(hasProductInCart === false){ if(jQuery('[data-variant-id="' + variantId + '"]').length > 0) { hasProductInCart = true; } } } else if(typeof window.Shopify.Checkout == "undefined") { jQuery.ajax({ url: "/cart.json?onetree=1", type: "GET", success: function (result) { if (result.items != undefined) { for (var i = 0; i < result.items.length; i++) { if (result.items[i].variant_id == variantId) { hasProductInCart = true; break; } } } } }); } }; var global = { jq: null, init: function () { this.eventListener(); }, loadScript: function (url, callback) { var script = document.createElement("script"); script.type = "text/javascript"; if (script.readyState) { script.onreadystatechange = function () { if (script.readyState === "loaded" || script.readyState === "complete") { script.onreadystatechange = null; callback(); } }; } else { script.onload = function () { callback(); }; } script.src = url; document.getElementsByTagName("head")[0].appendChild(script); }, appendContent: function (content) { this.jq(document.body).prepend(content); }, checkHasProductInCart: function (callback) { var self = this; self.jq.ajax({ url: "/cart.json?onetree=1", type: "GET", success: function (result) { var items = result.items; var hasProductInCart = false; if (items !== undefined) { for (var i = 0; i < items.length; i++) { if (parseInt(items[i].variant_id) === parseInt(variantId)) { hasProductInCart = true; break; } } } setTimeout(function () { callback(hasProductInCart) }, 0); } }); }, addProductToCart: function (callback) { let quantity = 1; const http = new XMLHttpRequest(); http.open('POST', '/cart/add.js', true); http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); http.onreadystatechange = function () { if (http.readyState === 4) { if (typeof callback === "function") { callback(); } } }; http.send("id=" + variantId + "&quantity=" + quantity); }, addProductToCartFromThankYouPage: function () { const http = new XMLHttpRequest(); http.open('POST', 'https://cloud.reforestationapp.com/thank-you-page-add-to-cart', true); http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); http.onreadystatechange = function () { if (http.readyState === 4) { if (http.status === 200) { const response = JSON.parse(http.responseText); if (response.success === true) { window.location.href = response.redirectUrl; } else { if (response.message.length > 0) { alert(response.message); } } } } }; http.send("shop=alltrailspartner.myshopify.com&date=" + new Date() + "&sessionId=" + sessionId + "&hash=" + hash + "&checkoutToken=" + checkout["token"] + "&orderId=" + checkout["order_id"]); }, deleteProductInCart: function () { this.jq.post('/cart/change.js', { quantity: 0, id: variantId, async: true }); }, eventListener: function () { var self = this; if ((typeof jQuery === 'undefined') || (typeof jQuery.ajax === 'undefined') || (parseFloat(jQuery.fn.jquery) < 1.7)) { self.loadScript('//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js', function () { var jQuery311 = jQuery.noConflict(true); self.jq = jQuery311; oneTreePlanted(jQuery311); }); } else { self.jq = jQuery; oneTreePlanted(jQuery); } } }; global.init(); })();