Created
May 29, 2023 10:23
-
-
Save jmcausing/af1be9cbfd25ddbca9f51107382890d3 to your computer and use it in GitHub Desktop.
CommerceKit - add to cart without opening the sidebar cart automatically.
This file contains hidden or 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
add_action( 'after_setup_theme', 'cg_remove_default_cart_drawer', 99 ); | |
function cg_remove_default_cart_drawer() { | |
remove_action( 'shoptimizer_before_site', 'shoptimizer_header_cart_drawer', 5 ); | |
add_action( 'shoptimizer_before_site', 'shoptimizer_header_cart_drawer_updated', 5 ); | |
} | |
if ( ! function_exists( 'shoptimizer_header_cart_drawer_updated' ) ) { | |
/** | |
* Display Header Cart Drawer | |
* | |
* @since 1.0.0 | |
* @uses shoptimizer_is_woocommerce_activated() check if WooCommerce is activated | |
* @return void | |
*/ | |
function shoptimizer_header_cart_drawer_updated() { | |
$shoptimizer_cart_title = shoptimizer_get_option( 'shoptimizer_cart_title' ); | |
if ( shoptimizer_is_woocommerce_activated() ) { | |
if ( is_cart() ) { | |
$class = 'current-menu-item'; | |
} else { | |
$class = ''; | |
} | |
?> | |
<div class="shoptimizer-mini-cart-wrap"> | |
<div id="ajax-loading"> | |
<div class="shoptimizer-loader"> | |
<div class="spinner"> | |
<div class="bounce1"></div> | |
<div class="bounce2"></div> | |
<div class="bounce3"></div> | |
</div> | |
</div> | |
</div> | |
<div class="cart-drawer-heading"><?php echo shoptimizer_safe_html( $shoptimizer_cart_title ); ?></div> | |
<div class="close-drawer"> | |
<span aria-hidden="true"> | |
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"></path></svg> | |
</span> | |
</div> | |
<?php the_widget( 'WC_Widget_Cart', 'title=' ); ?> | |
</div> | |
<?php | |
$shoptimizer_cart_drawer_js = ''; | |
$shoptimizer_cart_drawer_js .= " | |
document.addEventListener( 'DOMContentLoaded', function() { | |
document.addEventListener( 'click', function( event ) { | |
var is_inner = event.target.closest( '.shoptimizer-mini-cart-wrap' ); | |
if ( ! event.target.classList.contains( 'shoptimizer-mini-cart-wrap' ) && ! is_inner ) { | |
document.querySelector( 'body' ).classList.remove( 'drawer-open' ); | |
} | |
var is_inner2 = event.target.closest( '.cart-click' ); | |
if ( event.target.classList.contains( 'cart-click' ) || is_inner2 ) { | |
var is_header = event.target.closest( '.site-header-cart' ); | |
if ( is_header ) { | |
event.preventDefault(); | |
document.querySelector( 'body' ).classList.toggle( 'drawer-open' ); | |
} | |
} | |
if ( event.target.classList.contains( 'close-drawer' ) ) { | |
document.querySelector( 'body' ).classList.remove( 'drawer-open' ); | |
} | |
} ); | |
} ); | |
var interceptor = ( function( open ) { | |
XMLHttpRequest.prototype.open = function( method, url, async, user, pass ) { | |
this.addEventListener( 'readystatechange', function() { | |
switch ( this.readyState ) { | |
case 1: | |
document.querySelector( '#ajax-loading' ).style.display = 'block'; | |
break; | |
case 4: | |
document.querySelector( '#ajax-loading' ).style.display = 'none'; | |
break; | |
} | |
}, false ); | |
if ( async !== false ) { | |
async = true; | |
} | |
open.call( this, method, url, async, user, pass ); | |
}; | |
} ( XMLHttpRequest.prototype.open ) ); | |
document.addEventListener( 'DOMContentLoaded', function() { | |
document.querySelector( '#ajax-loading' ).style.display = 'none'; | |
} ); | |
"; | |
wp_add_inline_script( 'shoptimizer-main', $shoptimizer_cart_drawer_js ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment