(function () { if (document.getElementById("reforestation-app-load") !== null) { return; } document.querySelector("body").insertAdjacentHTML('beforeend', ''); var hash = 'B8pdgyBpUZ'; var sessionId = 'a247443c0d1a42473395b9140e1308e9'; var variantId = '44524720750834'; var checkout = window.Shopify && window.Shopify.checkout; var oneTreePlanted = function (jQuery) { jQuery('body').prepend(''); jQuery('[data-selector="one-tree-planted-tree-counter"]').html('429'); var hasProductInCart = false; if (typeof checkout !== "undefined") { checkout['line_items'].forEach(lineItem => { if(parseInt(lineItem.variant_id) === parseInt(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(parseInt(result.items[i].variant_id) === parseInt(variantId) || result.items[i].product_title === 'Tree to be Planted' || result.items[i].vendor === 'One Tree Planted' || result.items[i].product_title === 'Donation of a Tree' || result.items[i].vendor === 'Donation of a Tree') { 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) || items[i].product_title === 'Tree to be Planted' || items[i].vendor === 'One Tree Planted' || items[i].product_title === 'Donation of a Tree' || items[i].vendor === 'Donation of a Tree') { hasProductInCart = true; break; } } } setTimeout(function () { callback(hasProductInCart) }, 0); } }); }, addProductToCart: function (callback) { let quantity = 1; const http = new XMLHttpRequest(); http.open('POST', '/cart/add.js', false); 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://shopify.onetreeplanted.app/thank-you-page-add-to-cart', false); 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=growtentsdepot.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(); })();