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

@@ -70,35 +70,42 @@
/**
* Trigger the theme's minicart open mechanism.
*
* Strategy:
* 1. Try clicking the first element matching the configured selector.
* 2. If that element has no jQuery click handlers registered, dispatch
* a native 'click' so themes that use addEventListener also fire.
* Strategy (stops at the first approach that succeeds):
* 1. Find the header cart button via the configured selector list and
* fire ONE native click on it. Using native click (not jQuery trigger
* + native) avoids the double-fire / toggle-closed bug.
* 2. If no button is found, try to show the Shoptimizer cart panel
* directly by toggling the body class the theme uses.
* 3. Last resort: navigate to the cart URL.
*
* NOTE: never call both $el.trigger('click') AND el.click() — that
* fires two events and the toggle cancels itself immediately.
*/
openMinicart: function () {
// --- 1. Try clicking the configured header cart button ------------
var selectors = ( cgkitFC.minicartTrigger || '' ).split( ',' );
var $trigger = null;
for ( var i = 0; i < selectors.length; i++ ) {
var $el = $( $.trim( selectors[ i ] ) ).first();
if ( $el.length ) {
$trigger = $el;
break;
// One native click reaches both jQuery and native listeners.
$el[ 0 ].click();
return;
}
}
if ( $trigger && $trigger.length ) {
// Fire both jQuery and native click so all listeners catch it.
$trigger.trigger( 'click' );
var nativeEl = $trigger.get( 0 );
if ( nativeEl && typeof nativeEl.click === 'function' ) {
nativeEl.click();
}
// --- 2. Shoptimizer direct panel toggle --------------------------
// Shoptimizer opens its cart panel by adding the 'active' class to
// .site-header-cart and showing the child .widget_shopping_cart.
// Try that directly if we couldn't find the button.
var $panel = $( '.site-header-cart' );
if ( $panel.length ) {
$panel.toggleClass( 'active' );
$panel.find( '.widget_shopping_cart' ).toggleClass( 'cart-open' );
return;
}
// No trigger found → fall back to cart page
// --- 3. Fall back to cart page -----------------------------------
if ( cgkitFC.cartUrl ) {
window.location.href = cgkitFC.cartUrl;
}

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';
}
/**