( function( $ ) {
/**
* Hook to $( window ).load() to correctly capture the rendered attribute values.
* @since 3.5.1.2
*/
$( window ).ready( function() {
/**
* Payment Method selection.
* Expand the first Payment Method in Payment Page.
*/
setTimeout( function() {
let selected_method = $( 'input.tc_choose_gateway:checked' ).val(),
max_height = $( 'div#' + selected_method + ' > .inner-wrapper' ).outerHeight();
$( 'div#' + selected_method ).css( { 'max-height': max_height + 'px' } );
}, 500 );
});
$( document ).ready( function() {
const tc_cart = {
quantity: function() {
var quantity = 0;
$( 'input[name="ticket_quantity[]"]' ).each( function() {
quantity = quantity + parseInt( $( this ).val() );
} );
return quantity;
},
/**
* Initialize Listeners
*/
init: function() {
tc_cart.tc_empty_cart();
tc_cart.tc_cart_listeners();
},
/**
* Listeners for add item to cart
*
* @returns {undefined}
*/
tc_cart_listeners: function() {
$( 'body' ).on( 'click', 'input.tc_button_addcart', function() {
var input = $( this ),
formElm = $( input ).parents( 'form.tc_buy_form' ),
tempHtml = formElm.html(),
serializedForm = formElm.serialize();
formElm.html( '' );
$.post( tc_ajax.ajaxUrl, serializedForm, function( data ) {
var result = data.split( '||', 2 );
if ( 'error' == result[ 0 ] ) {
alert( result[ 1 ] );
formElm.html( tempHtml );
tc_cart.tc_cart_listeners();
} else {
formElm.html( '' + tc_ajax.successMsg + '' );
$( 'div.tc_cart_widget_content' ).html( result[ 1 ] );
if ( result[ 0 ] > 0 ) {
formElm.fadeOut( 2000, function() {
formElm.html( tempHtml ).fadeIn( 'fast' );
tc_cart.tc_cart_listeners();
} );
} else {
formElm.fadeOut( 2000, function() {
formElm.html( '' + tc_ajax.outMsg + '' ).fadeIn( 'fast' );
tc_cart.tc_cart_listeners();
} );
}
tc_cart.tc_empty_cart(); // Re-init empty script as the widget was reloaded
}
} );
return false;
} );
},
/**
* Empty Cart
*
* @returns {undefined}
*/
tc_empty_cart: function() {
if ( $( 'a.tc_empty_cart' ).attr( 'onClick' ) != undefined ) {
return;
}
$( 'body' ).on( 'click', 'a.tc_empty_cart', function() {
var answer = confirm( tc_ajax.empty_cart_message );
if ( answer ) {
$( this ).html( '
' );
$.post( tc_ajax.ajaxUrl, { action: 'mp-update-cart', empty_cart: 1 }, function( data ) {
$( 'div.tc_cart_widget_content' ).html( data );
} );
}
return false;
} );
}
};
/**
* Check age restriction.
* Woocommerce + Bridge for Woocommerce
*/
$( 'form.checkout' ).on( 'checkout_place_order', function( event ) {
if ( $( '#tc_age_check' ).length !== 0 ) {
if ( false == $( '#tc_age_check' ).is( ':checked' ) ) {
$( '.tc-age-check-error' ).remove();
$( '.tc-age-check-label' ).append( '' + tc_ajax.tc_error_message + '' );
$( 'html, body' ).stop().animate( { 'scrollTop': ( $( '#tc_age_check' ).offset().top ) - 100 }, 350, 'swing', function() { window.location.hash = target; });
return false;
}
}
} );
if ( $( '.tc_cart_widget' ).length > 0 ) {
function tc_update_cart_ajax() {
$( '.tc_cart_ul' ).css( 'opacity', '0.5' );
// Since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.post( tc_ajax.ajaxUrl, {
action: 'tickera_update_widget_cart',
nonce: tc_ajax.ajaxNonce
}, function( response ) {
$( '.tc_cart_ul' ).css( 'opacity', '1' );
$( '.tc_cart_ul' ).html( '' );
$( '.tc_cart_ul' ).html( response );
} );
}
// Listen DOM changes
$( '.event_tickets, .cart_form' ).bind( 'DOMSubtreeModified', tc_update_cart_ajax );
}
$( document ).on( 'submit', '#tc_payment_form', function() {
$( '#tc_payment_confirm' ).attr( 'disabled', 'disabled' );
} );
/**
* Increase the quantity
*/
$( 'body' ).on( 'click', 'input.tickera_button.plus', function() {
let parentContainer = $( this ).closest( 'td' ),
quantity = parentContainer.find( '.quantity' ).val();
parentContainer.find( '.quantity' ).val( parseInt( quantity ) + 1 );
} );
/**
* Decrease the quantity
*/
$( 'body' ).on( 'click', 'input.tickera_button.minus', function() {
let parentContainer = $( this ).closest( 'td' ),
quantity = parentContainer.find( '.quantity' ).val();
if ( quantity >= 1 ) {
parentContainer.find( '.quantity' ).val( parseInt( quantity ) - 1 );
}
} );
/**
* When user clicks on the empty cart button
*/
$( 'body' ).on( 'click', '#empty_cart', function( event ) {
let proceed = confirm( tc_ajax.empty_cart_confirmation );
if ( proceed ) {
$( 'input[name="cart_action"]' ).val( 'empty_cart' );
} else {
event.preventDefault();
}
} );
/**
* When user clicks on the update button
*/
$( 'body' ).on( 'click', '#update_cart', function() {
$( 'input[name="cart_action"]' ).val( 'update_cart' );
} );
/**
* Toggle Customer Age Checkbox
*/
$( document ).on( 'change', '#tc_age_check', function() {
if ( $( this ).is( ':checked' ) ) {
$( this ).removeClass( 'has-error' );
$( '.tc-age-check-error' ).remove();
} else {
$( this ).addClass( 'has-error' );
$( '.tc-age-check-error' ).remove();
$( this ).parent().append( '' + tc_ajax.tc_error_message + '' );
}
} );
/**
* Tickera Standalone
* When user click on the proceed to checkout button,
*/
var current_quantity = tc_cart.quantity();
$( document ).on( 'click', '#proceed_to_checkout', function( event ) {
// Make sure to update the cart if there's some changes before moving to checkout page.
let input_quantity = tc_cart.quantity();
if ( typeof tc_cart.quantity() === 'undefined' || 0 == tc_cart.quantity() || current_quantity != input_quantity ) {
event.preventDefault();
let target = $( '.tc_cart_errors' );
if ( target.find( 'ul' ).length ) {
target.find( 'ul' ).append( '