Created
July 23, 2024 07:28
-
-
Save xlplugins/07e0b8277994832f368ed0e4e9e10f26 to your computer and use it in GitHub Desktop.
Funnelkit Cart: Trigger Add to cart Slide Button when cart is not empty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FKCART_Trigger_Slide_Cart_When_Cart_Is_Non_Empty { | |
public function __construct() { | |
add_action( 'wp_footer', [ $this, 'action' ] ); | |
add_filter( 'body_class', function ( $body_class ) { | |
$body_class[] = 'fkcart_empty'; | |
return $body_class; | |
} ); | |
} | |
public function action() { | |
?> | |
<style> | |
.fkcart_empty #fkcart-mini-toggler .fkcart-shortcode-price { | |
display: none; | |
} | |
</style> | |
<script> | |
window.addEventListener('load', function () { | |
(function ($) { | |
setTimeout(function() { | |
if (typeof jQuery === 'undefined') { | |
console.log('jQuery not found'); | |
return; | |
} | |
if(!$('body').hasClass('fkcart_empty')){ | |
console.log("tester"); | |
$('body').trigger('fkcart_update_side_cart', [true]); | |
} | |
}, 1500); | |
let qty = getCookie('fkcart_cart_qty'); | |
if (typeof qty == "undefined" || qty === "" || isNaN(qty)) { | |
add_count_class(0); | |
return; | |
} | |
setTimeout(function(){ | |
add_count_class(qty); | |
},100); | |
$(document.body).on('added_to_cart', function () { | |
setTimeout(() => { | |
let qty = getCookie('fkcart_cart_qty'); | |
add_count_class(qty); | |
}, 500); | |
}); | |
$(document.body).on('removed_from_cart', function () { | |
setTimeout(() => { | |
let qty = getCookie('fkcart_cart_qty'); | |
add_count_class(qty); | |
}, 500); | |
}); | |
function add_count_class(qty) { | |
qty = parseFloat(qty, 10); | |
if (qty == 0) { | |
$('body').addClass('fkcart_empty'); | |
} else { | |
$('body').removeClass('fkcart_empty'); | |
} | |
} | |
function getCookie(cookieName) { | |
let cookie = {}; | |
document.cookie.split(';').forEach(function (el) { | |
let [key, value] = el.split('='); | |
cookie[key.trim()] = value; | |
}) | |
return cookie[cookieName]; | |
} | |
})(jQuery); | |
}); | |
</script> | |
<?php | |
} | |
} | |
new FKCART_Trigger_Slide_Cart_When_Cart_Is_Non_Empty(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment