Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xlplugins/823171a4197097d8062faa037b3a03bb to your computer and use it in GitHub Desktop.
Save xlplugins/823171a4197097d8062faa037b3a03bb to your computer and use it in GitHub Desktop.
Make cart empty if user first open the dedicated Funnel Checkout page and left without placing an order
class FKCART_Trigger_Add_To_cart_On_Page_Load {
public function __construct() {
add_action( 'wp_footer', [ $this, 'add_js' ] );
add_filter('fkcart_re_run_get_slide_cart_ajax', '__return_true', 9999);
add_action( 'wp_head', [$this,'action'], 99999);
}
public function action() {
if ( ! class_exists( 'WFACP_Core' ) || ! function_exists( 'WC' ) ) {
return;
}
if ( ! WFACP_Core()->public instanceof WFACP_Public ) {
return;
}
if ( ! WC()->cart || WC()->cart->is_empty() ) {
echo "tester";
return;
}
$is_global_checkout = WFACP_Core()->public->is_checkout_override();
if ( false == $is_global_checkout && did_action('wfacp_checkout_page_not_found') == 0 ) {
return;
}
$cart = WC()->cart->cart_contents;
$items_to_remove = array();
foreach ( $cart as $key => $item ) {
if ( isset( $item['_wfacp_product'] ) || isset( $item['_wfob_product'] ) ) {
$items_to_remove[] = $key;
}
}
// Remove items outside the foreach loop to avoid issues
foreach ( $items_to_remove as $key ) {
WC()->cart->remove_cart_item( $key );
}
}
public function add_js() {
?>
<script>
window.addEventListener('load', function () {
function update_side_cart(){
setTimeout(function(){
console.log("update_side_cart");
jQuery('body').trigger('fkcart_update_side_cart', [false]);
},500);
}
jQuery(document.body).on('fkcart_cart_open', function () {
update_side_cart();
});
update_side_cart();
});
// Enhanced pageshow event to reload page when back button is pressed to shop page
window.addEventListener('pageshow', function (event) {
// Check if page was loaded from cache (back/forward button)
if (event.persisted || (window.performance && window.performance.navigation.type === 2)) {
if(jQuery('body').hasClass('woocommerce-shop')){
// Force reload the page
window.location.reload(true);
}
}
});
</script>
<?php
}
}
new FKCART_Trigger_Add_To_cart_On_Page_Load();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment