/** * BOGO public side JS file. * Free product selection functionality. * * @since 3.0.0 * @package Wt_Smart_Coupon */ jQuery( function ($) { "use strict"; $( document ).on( 'change', '.wbte_give_away_product_attr', function () { var attributes = {}; var parent = $( this ).closest( '.wbte_get_away_product' ); parent.find( '.wbte_give_away_product_attr' ).each( function (index) { attributes[$( this ).attr( 'data-attribute_name' )] = $( this ).val(); } ); const coupon_id = parent.closest( '.wbte_sc_bogo_products' ).attr( 'coupon' ); if ( "" == $( this ).val() ) { parent.find( 'input[name="variation_id"]' ).val( 0 ); parent.find( 'input[name="wt_variation_options"]' ).val( JSON.stringify( attributes ) ); return false; } var stop_checking = false; $.each( attributes, function ( key, value ) { if ( value == '' ) { stop_checking = true; return false; } } ); if ( stop_checking ) { /** Not every attributes selected. */ return; } var product_id = parent.attr( 'product-id' ); var data = { 'attributes' : attributes, 'product' : product_id, '_wpnonce' : WTSmartCouponOBJ.nonces.public, 'coupon_id' : coupon_id }; $( '.wbte_sc_bogo_add_to_cart, .checkout-button, .wbte_sc_bogo_proceed_checkout' ).prop( 'disabled', true ).css( {'opacity':.5, 'cursor':'not-allowed'} ); jQuery.ajax( { type: "POST", async: true, url: WTSmartCouponOBJ.wc_ajax_url + 'update_variation_id_on_choose', data: data, dataType: 'json', success:function (response) { const productList = parent.closest( '.wbte_sc_bogo_products' ); if ( true === response.status ) { parent.find( 'input[name="variation_id"]' ).val( response.variation_id ); parent.find( 'input[name="wt_variation_options"]' ).val( JSON.stringify( attributes ) ); parent.find('.wbte_sc_bogo_variation_error').remove(); wbteScUpdateSelectedCount( productList ); if ( response.img_url && '' !== response.img_url ) { parent.find( '.wbte_product_image img' ).attr( 'src', response.img_url ); } if ( response.price && '' !== response.price && response.discount && '' !== response.discount ) { parent.find( '.wbte_sc_bogo_product_price' ).html( response.price ); parent.find( '.wbte_sc_bogo_product_discount_price' ).html( response.price - response.discount ); } if ( response.discount && '' !== response.discount ) { parent.find( '.wbte_sc_bogo_product_discount_price' ).html( response.discount ); } } else { wbteScUpdateSelectedCount( productList ); parent.find( 'input[name="variation_id"]' ).val( 0 ); if (false !== response.status_msg && "" !== response.status_msg.trim()) { /* check message was disabled or not */ alert( response.status_msg ); } } }, error:function () { parent.find( 'input[name="variation_id"]' ).val( 0 ); $( '.wbte_sc_bogo_add_to_cart, .checkout-button, .wbte_sc_bogo_proceed_checkout' ).prop( 'disabled', false ).css( {'opacity':1, 'cursor':'pointer'} ); $( '.wbte_get_away_product' ).css( {'opacity':1, 'cursor':'default'} ); alert( WTSmartCouponOBJ.labels.error ); } } ); } ); $( document ).ready( function() { /** Show popup when clicking the button */ $( document ).on( 'click', '.wbte_sc_bogo_popup_btn', function( e ) { e.preventDefault(); $( '.wbte_sc_giveaway_popup' ).fadeIn( 300 ); $( 'body' ).addClass( 'wbte_sc_popup_open' ); } ); /** Close popup when clicking outside */ $( document ).on( 'click', '.wbte_sc_giveaway_popup', function( e ) { if ( $( e.target ).hasClass( 'wbte_sc_giveaway_popup' ) ) { $( '.wbte_sc_giveaway_popup' ).fadeOut( 300 ); $( 'body' ).removeClass( 'wbte_sc_popup_open' ); } } ); /** Close popup when clicking close button */ $( document ).on( 'click', '.wbte_sc_giveaway_popup_close', function( e ) { e.preventDefault(); $( '.wbte_sc_giveaway_popup' ).fadeOut( 300 ); $( 'body' ).removeClass( 'wbte_sc_popup_open' ); } ); /** Handle product selection */ $( document ).on( 'click', '.wbte_get_away_product', function( e ) { if ( $( e.target ).is( 'select, input, option' ) || $( e.target ).closest( 'select, input' ).length ) { return; } const parent = $( this ); const productList = parent.closest( '.wbte_sc_bogo_products' ); const isAnyCondition = 'any' === productList.data( 'condition' ); const totalQty = Number.parseInt( productList.closest( '.wbte_sc_bogo_giveaway_products_container' ).find( '.giveaway-title' ).attr( 'data-total-qty' ) ); if ( '1' === parent.attr( 'data-is_purchasable' ) ) { const currentSelectedQty = wbteScGetSelectedQuantityTotal( productList ); if ( ! parent.hasClass( 'selected' ) ) { if ( currentSelectedQty >= totalQty ) { return; } parent.find( 'input[name="wbte_sc_bogo_quantity"]' ).val( 1 ); if ( isAnyCondition ) { /** For 'any' condition, deselect any previously selected product */ productList.find( '.wbte_get_away_product.selected' ).each( function() { $( this ).removeClass( 'selected' ); } ); parent.find( 'input[name="wbte_sc_bogo_quantity"]' ).val( totalQty ); } if ( 0 < parent.attr( 'data-free-qty-on-select' ) ) { parent.find( 'input[name="wbte_sc_bogo_quantity"]' ).val( parent.attr( 'data-free-qty-on-select' ) ); } } parent.toggleClass( 'selected' ); const variation_id = parent.find( '[name="variation_id"]' ).val() || 0; if ( 0 < parent.find( '.wbte_give_away_product_attr' ).length && ( '' === variation_id || '0' === variation_id ) ) { parent.find( '.wbte_sc_bogo_variation_error' ).remove(); const errorHtml = '
' + WTSmartCouponOBJ.labels.choose_variation + '
'; parent.find( '.wt_variations' ).after( errorHtml ); } if ( ! parent.hasClass( 'selected' ) ) { parent.find( '.wbte_