Skip to content

Instantly share code, notes, and snippets.

@ashukasma
Created December 10, 2021 07:15
Show Gist options
  • Save ashukasma/3bfca843a7ee97ddbd4cf0aeabdf02e6 to your computer and use it in GitHub Desktop.
Save ashukasma/3bfca843a7ee97ddbd4cf0aeabdf02e6 to your computer and use it in GitHub Desktop.
Simply Insurance - Hey Senior Side cart integration
<!-- simply-insurance -->
{%- assign sidecart = shop.metafields.SimplyInsurance.sidecart -%}
{%- assign sidecart = false -%}
<script>
window.simplyInsurance = window.simplyInsurance || {};
window.cn = function (o) { return "undefined" == typeof o || null == o || "" == o.toString().trim() };
simplyInsurance.insurancePlan = {{ shop.metafields.SimplyInsurance.InsurancePlan | json }};
simplyInsurance.insurancePlan = JSON.parse(simplyInsurance.insurancePlan);
simplyInsurance.money_format = "{{ shop.money_format }}";
simplyInsurance.cartJson = {{ cart | json }};
simplyInsurance.simplySelector = ".route-div";
{% comment %} simplyInsurance.simplySelector = ".si-widget"; {% endcomment %}
function checkInsure(){
if(cn(simplyInsurance.insurancePlan)){
return;
}
}
checkInsure();
/* Load css */
var cssId = 'myCss'; // you could encode the css path itself to generate id..
if (!document.getElementById(cssId)) {
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.id = cssId;
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = "{{'simply-insurance.css' | asset_url}}";
link.media = 'all';
head.appendChild(link);
}
</script>
<style>
.simply-insurance{
display: none;
}
.hs-item-cart-content.hs-product-39682313420843 {
display: none;
}
</style>
<script src="{{'simply-insurance.js' | asset_url}}" type="text/javascript" defer="defer"></script>
<script>
simplyInsurance.fetchEventListen = () => {
var get = window.fetch;
window.fetch = (...args) => {
if (args[0].includes('/add.js') || args[0].includes('/change.js')) {
setTimeout(function () {
try{
if(simplyInsurance && typeof simplyInsurance !== "undefined" )
simplyInsurance.loadApp();
}catch(e){
console.log(e);
}
}, 1000);
}
return get(...args);
};
}
simplyInsurance.ajaxEventListen = () => {
let open = window.XMLHttpRequest.prototype.open,
send = window.XMLHttpRequest.prototype.send;
function openReplacement(method, url, async, user, password) {
this._url = url;
return open.apply(this, arguments);
}
function sendReplacement(data) {
if (this.onreadystatechange) {
this._onreadystatechange = this.onreadystatechange;
}
this.onreadystatechange = onReadyStateChangeReplacement;
return send.apply(this, arguments);
}
function onReadyStateChangeReplacement() {
/**
* PLACE HERE YOUR CODE FOR READYSTATECHANGE
*/
if ((this._url.includes('/change.js') || this._url.includes('/add.js')) && this.response && this.readyState === 4) {
try{
if(simplyInsurance && typeof simplyInsurance !== "undefined" )
simplyInsurance.loadApp();
}catch(e){
console.log(e);
}
}
if (this._onreadystatechange) {
return this._onreadystatechange.apply(this, arguments);
}
}
window.XMLHttpRequest.prototype.open = openReplacement;
window.XMLHttpRequest.prototype.send = sendReplacement;
}
simplyInsurance.html = '<div class="simply-insurance widget"><div class="header-wrap"><h3>##plan_title</h3><div class="switch-button"><input hidden="hidden" class="cart-insurance-input" type="checkbox" ><label class="switch" for="cart-insurance-input"></label></div></div><p>##plan_description</p></div>';
simplyInsurance.cartReload = function(changeType){
switch(changeType){
case 0: {// insurance added
// location.reload();
if(typeof window.HSSLIDECART !== 'undefined') {
HsCartDrawer.updateSlideCart();
}
break;
}
case 1: { // insurance removed
// location.reload();
if(typeof window.HSSLIDECART !== 'undefined') {
HsCartDrawer.updateSlideCart();
}
break;
}
}
}
simplyInsurance.ajaxEventListen();
simplyInsurance.fetchEventListen();
setTimeout(function () {
try{
if(simplyInsurance && typeof simplyInsurance !== "undefined" )
simplyInsurance.loadApp();
}catch(e){
console.log(e);
}
}, 5000);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment