laipower/wp-content/plugins/gp-premium/woocommerce/functions/js/woocommerce.js

278 lines
7.6 KiB
JavaScript
Raw Normal View History

2020-04-07 13:03:04 +00:00
jQuery( document ).ready( function( $ ) {
var throttle = function(fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;
var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
// hold on to it
clearTimeout(deferTimer);
deferTimer = setTimeout(function () {
last = now;
fn.apply(context, args);
}, threshhold);
} else {
last = now;
fn.apply(context, args);
}
};
};
$( 'body' ).on( 'added_to_cart', function() {
if ( ! $( '.wc-menu-item' ).hasClass( 'has-items' ) ) {
$( '.wc-menu-item' ).addClass( 'has-items' );
}
if ( ! $( '.wc-mobile-cart-items' ).hasClass( 'has-items' ) ) {
$( '.wc-mobile-cart-items' ).addClass( 'has-items' );
}
} );
$( 'body' ).on( 'removed_from_cart', function() {
var numberOfItems = $( '.number-of-items' );
if ( numberOfItems.length ) {
if ( numberOfItems.hasClass( 'no-items' ) ) {
$( '.wc-menu-item' ).removeClass( 'has-items' );
$( '.wc-mobile-cart-items' ).removeClass( 'has-items' );
}
}
} );
if ( generateWooCommerce.addToCartPanel ) {
$( document.body ).on( "added_to_cart", function() {
var adminBar = $( '#wpadminbar' ),
stickyNav = $( '.navigation-stick' ),
top = 0;
if ( adminBar.length ) {
top = adminBar.outerHeight();
}
if ( stickyNav.length && '0px' === stickyNav.css( 'top' ) ) {
top = top + stickyNav.outerHeight();
}
$( '.add-to-cart-panel' ).addClass( 'item-added' ).css( {
'-webkit-transform': 'translateY(' + top + 'px)',
'-ms-transform': 'translateY(' + top + 'px)',
'transform': 'translateY(' + top + 'px)'
} );
} );
$( '.add-to-cart-panel .continue-shopping' ).on( 'click', function( e ) {
e.preventDefault();
$( '.add-to-cart-panel' ).removeClass( 'item-added' ).css( {
'-webkit-transform': 'translateY(-100%)',
'-ms-transform': 'translateY(-100%)',
'transform': 'translateY(-100%)'
} );
} );
$( window ).on( 'scroll', throttle( function() {
var panel = $( '.add-to-cart-panel' );
if ( panel.hasClass( 'item-added' ) ) {
panel.removeClass( 'item-added' ).css( {
'-webkit-transform': 'translateY(-100%)',
'-ms-transform': 'translateY(-100%)',
'transform': 'translateY(-100%)'
} );
}
}, 250 ) );
}
if ( generateWooCommerce.stickyAddToCart ) {
var lastScroll = 0;
$( window ).on( 'scroll', throttle( function() {
var adminBar = $( '#wpadminbar' ),
stickyNav = $( '.navigation-stick' ),
top = 0,
scrollTop = $( window ).scrollTop(),
panel = $( '.add-to-cart-panel' ),
panelPosition = panel.offset().top + panel.outerHeight(),
button = $( '.single_add_to_cart_button' ),
buttonTop = button.offset().top,
buttonHeight = button.outerHeight(),
footerTop = $( '.site-footer' ).offset().top;
if ( adminBar.length ) {
top = adminBar.outerHeight();
}
if ( stickyNav.length ) {
if ( stickyNav.hasClass( 'auto-hide-sticky' ) ) {
if ( scrollTop < lastScroll && '0px' === stickyNav.css( 'top' ) ) {
top = top + stickyNav.outerHeight();
} else {
top = top;
}
lastScroll = scrollTop;
} else {
top = top + stickyNav.outerHeight();
}
}
if ( scrollTop > ( buttonTop + buttonHeight ) && panelPosition < footerTop ) {
panel.addClass( 'show-sticky-add-to-cart' ).css( {
'-webkit-transform': 'translateY(' + top + 'px)',
'-ms-transform': 'translateY(' + top + 'px)',
'transform': 'translateY(' + top + 'px)'
} );
} else {
panel.removeClass( 'show-sticky-add-to-cart' ).css( {
'-webkit-transform': '',
'-ms-transform': '',
'transform': ''
} );
}
}, 250 ) );
$( '.go-to-variables' ).on( 'click', function( e ) {
e.preventDefault();
var offset = 0,
stickyNav = $( '.navigation-stick' ),
adminBar = $( '#wpadminbar' );
if ( stickyNav.length ) {
offset = stickyNav.outerHeight();
}
if ( adminBar.length ) {
offset = offset + adminBar.outerHeight();
}
$( 'html, body' ).animate({
scrollTop: $( '.variations' ).offset().top - offset
}, 250 );
} );
}
$( function() {
2020-04-07 13:03:04 +00:00
"use strict";
if ( generateWooCommerce.quantityButtons ) {
generateQuantityButtons();
}
} );
$( document ).ajaxComplete( function() {
"use strict";
if ( generateWooCommerce.quantityButtons ) {
generateQuantityButtons();
}
} );
function generateQuantityButtons() {
2020-04-07 13:03:04 +00:00
var quantityBoxes,
cart = $( '.woocommerce div.product form.cart' );
if ( cart.closest( '.elementor-add-to-cart' ).length ) {
$( '.elementor.product' ).removeClass( 'do-quantity-buttons' );
return;
}
quantityBoxes = $( '.cart div.quantity:not(.buttons-added), .cart td.quantity:not(.buttons-added)' ).find( '.qty' );
2020-04-07 13:03:04 +00:00
if ( quantityBoxes ) {
$.each( quantityBoxes, function( key, value ) {
var box = $( value );
2020-04-07 13:03:04 +00:00
if ( 'date' !== box.prop( 'type' ) && 'hidden' !== box.prop( 'type' ) ) {
// Add plus and minus icons
box.parent().addClass( 'buttons-added' ).prepend('<a href="javascript:void(0)" class="minus">-</a>');
box.after('<a href="javascript:void(0)" class="plus">+</a>');
2020-04-07 13:03:04 +00:00
// Target quantity inputs on product pages
$( 'input.qty:not(.product-quantity input.qty)' ).each( function() {
var min = parseFloat( $( this ).attr( 'min' ) );
2020-04-07 13:03:04 +00:00
if ( min && min > 0 && parseFloat( $( this ).val() ) < min ) {
$( this ).val( min );
}
} );
2020-04-07 13:03:04 +00:00
// Quantity input
if ( $( 'body' ).hasClass( 'single-product' ) && ! cart.hasClass( 'grouped_form' ) ) {
var quantityInput = $( '.woocommerce form input[type=number].qty' );
2020-04-07 13:03:04 +00:00
quantityInput.on( 'keyup', function() {
var qty_val = $( this ).val();
quantityInput.val( qty_val );
} );
}
2020-04-07 13:03:04 +00:00
$( '.plus, .minus' ).off( 'click' );
2020-04-07 13:03:04 +00:00
$( '.plus, .minus' ).on( 'click', function() {
// Quantity
var quantityBox;
2020-04-07 13:03:04 +00:00
// If floating bar is enabled
if ( $( 'body' ).hasClass( 'single-product' ) && ! cart.hasClass( 'grouped_form' ) && ! cart.hasClass( 'cart_group' ) ) {
quantityBox = $( '.plus, .minus' ).closest( '.quantity' ).find( '.qty' );
} else {
quantityBox = $( this ).closest( '.quantity' ).find( '.qty' );
}
2020-04-07 13:03:04 +00:00
// Get values
var currentQuantity = parseFloat( quantityBox.val() ),
maxQuantity = parseFloat( quantityBox.attr( 'max' ) ),
minQuantity = parseFloat( quantityBox.attr( 'min' ) ),
step = quantityBox.attr( 'step' );
2020-04-07 13:03:04 +00:00
// Fallback default values
if ( ! currentQuantity || '' === currentQuantity || 'NaN' === currentQuantity ) {
currentQuantity = 0;
}
2020-04-07 13:03:04 +00:00
if ( '' === maxQuantity || 'NaN' === maxQuantity ) {
maxQuantity = '';
}
2020-04-07 13:03:04 +00:00
if ( '' === minQuantity || 'NaN' === minQuantity ) {
minQuantity = 0;
}
2020-04-07 13:03:04 +00:00
if ( 'any' === step || '' === step || undefined === step || 'NaN' === parseFloat( step ) ) {
step = 1;
}
2020-04-07 13:03:04 +00:00
// Change the value
if ( $( this ).is( '.plus' ) ) {
2020-04-07 13:03:04 +00:00
if ( maxQuantity && ( maxQuantity == currentQuantity || currentQuantity > maxQuantity ) ) {
quantityBox.val( maxQuantity );
} else {
quantityBox.val( currentQuantity + parseFloat( step ) );
}
2020-04-07 13:03:04 +00:00
} else {
2020-04-07 13:03:04 +00:00
if ( minQuantity && ( minQuantity == currentQuantity || currentQuantity < minQuantity ) ) {
quantityBox.val( minQuantity );
} else if ( currentQuantity > 0 ) {
quantityBox.val( currentQuantity - parseFloat( step ) );
}
2020-04-07 13:03:04 +00:00
}
2020-04-07 13:03:04 +00:00
// Trigger change event
quantityBox.trigger( 'change' );
} );
}
2020-04-07 13:03:04 +00:00
} );
}
}
});