fix: disable WC cart redirect, enable AJAX add-to-cart, fix double-click toggle bug

This commit is contained in:
2026-03-05 08:08:18 +01:00
parent dbf48a692d
commit 022d782ec7
2 changed files with 48 additions and 17 deletions

View File

@@ -3,7 +3,7 @@
* Plugin Name: CommerceKit Floating Cart
* Plugin URI: https://www.commercegurus.com
* Description: Adds a floating cart icon (bottom-right) and auto-opens the CommerceKit minicart after add to cart. Requires CommerceGurus CommerceKit and WooCommerce.
* Version: 1.0.0
* Version: 1.0.1
* Author: CommerceGurus
* Author URI: https://www.commercegurus.com
* License: GPLv3
@@ -19,7 +19,7 @@ if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'CGKIT_FC_VERSION', '1.0.0' );
define( 'CGKIT_FC_VERSION', '1.0.1' );
define( 'CGKIT_FC_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'CGKIT_FC_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
@@ -50,6 +50,30 @@ class CommerceKit_Floating_Cart {
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_assets' ] );
add_action( 'wp_footer', [ $this, 'render_floating_cart' ] );
add_filter( 'woocommerce_add_to_cart_fragments', [ $this, 'cart_count_fragment' ] );
// Disable WooCommerce's "redirect to cart page after add" so the page
// stays put and we can open the minicart instead.
add_filter( 'pre_option_woocommerce_cart_redirect_after_add', [ $this, 'disable_cart_redirect' ] );
// Ensure WooCommerce's AJAX add-to-cart is active so the body receives
// the `added_to_cart` JS event that our script listens for.
add_filter( 'pre_option_woocommerce_enable_ajax_add_to_cart', [ $this, 'enable_ajax_add_to_cart' ] );
}
/**
* Return 'no' so WooCommerce never redirects to /cart after adding a product.
*/
public function disable_cart_redirect( string $value ): string {
return 'no';
}
/**
* Return 'yes' so WooCommerce uses AJAX add-to-cart on archive/shop pages.
* This ensures `added_to_cart` JS event fires without a page reload.
*/
public function enable_ajax_add_to_cart( string $value ): string {
// Only override when it's not already enabled.
return ( 'yes' === $value ) ? $value : 'yes';
}
/**