fix: disable WC cart redirect, enable AJAX add-to-cart, fix double-click toggle bug
This commit is contained in:
@@ -70,35 +70,42 @@
|
|||||||
/**
|
/**
|
||||||
* Trigger the theme's minicart open mechanism.
|
* Trigger the theme's minicart open mechanism.
|
||||||
*
|
*
|
||||||
* Strategy:
|
* Strategy (stops at the first approach that succeeds):
|
||||||
* 1. Try clicking the first element matching the configured selector.
|
* 1. Find the header cart button via the configured selector list and
|
||||||
* 2. If that element has no jQuery click handlers registered, dispatch
|
* fire ONE native click on it. Using native click (not jQuery trigger
|
||||||
* a native 'click' so themes that use addEventListener also fire.
|
* + 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.
|
* 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 () {
|
openMinicart: function () {
|
||||||
|
// --- 1. Try clicking the configured header cart button ------------
|
||||||
var selectors = ( cgkitFC.minicartTrigger || '' ).split( ',' );
|
var selectors = ( cgkitFC.minicartTrigger || '' ).split( ',' );
|
||||||
var $trigger = null;
|
|
||||||
|
|
||||||
for ( var i = 0; i < selectors.length; i++ ) {
|
for ( var i = 0; i < selectors.length; i++ ) {
|
||||||
var $el = $( $.trim( selectors[ i ] ) ).first();
|
var $el = $( $.trim( selectors[ i ] ) ).first();
|
||||||
if ( $el.length ) {
|
if ( $el.length ) {
|
||||||
$trigger = $el;
|
// One native click – reaches both jQuery and native listeners.
|
||||||
break;
|
$el[ 0 ].click();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $trigger && $trigger.length ) {
|
// --- 2. Shoptimizer direct panel toggle --------------------------
|
||||||
// Fire both jQuery and native click so all listeners catch it.
|
// Shoptimizer opens its cart panel by adding the 'active' class to
|
||||||
$trigger.trigger( 'click' );
|
// .site-header-cart and showing the child .widget_shopping_cart.
|
||||||
var nativeEl = $trigger.get( 0 );
|
// Try that directly if we couldn't find the button.
|
||||||
if ( nativeEl && typeof nativeEl.click === 'function' ) {
|
var $panel = $( '.site-header-cart' );
|
||||||
nativeEl.click();
|
if ( $panel.length ) {
|
||||||
}
|
$panel.toggleClass( 'active' );
|
||||||
|
$panel.find( '.widget_shopping_cart' ).toggleClass( 'cart-open' );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// No trigger found → fall back to cart page
|
// --- 3. Fall back to cart page -----------------------------------
|
||||||
if ( cgkitFC.cartUrl ) {
|
if ( cgkitFC.cartUrl ) {
|
||||||
window.location.href = cgkitFC.cartUrl;
|
window.location.href = cgkitFC.cartUrl;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
* Plugin Name: CommerceKit Floating Cart
|
* Plugin Name: CommerceKit Floating Cart
|
||||||
* Plugin URI: https://www.commercegurus.com
|
* 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.
|
* 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: CommerceGurus
|
||||||
* Author URI: https://www.commercegurus.com
|
* Author URI: https://www.commercegurus.com
|
||||||
* License: GPLv3
|
* License: GPLv3
|
||||||
@@ -19,7 +19,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||||||
exit;
|
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_URL', plugin_dir_url( __FILE__ ) );
|
||||||
define( 'CGKIT_FC_PLUGIN_DIR', plugin_dir_path( __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_enqueue_scripts', [ $this, 'enqueue_assets' ] );
|
||||||
add_action( 'wp_footer', [ $this, 'render_floating_cart' ] );
|
add_action( 'wp_footer', [ $this, 'render_floating_cart' ] );
|
||||||
add_filter( 'woocommerce_add_to_cart_fragments', [ $this, 'cart_count_fragment' ] );
|
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';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user