(function($){ $(document).ready(function(){ $('body').on('change','.checkout .input-radio',function(){ $('body').trigger('update_checkout'); }); }); // Repeaters $( '.wcmo_repeat_field' ).on( 'click', function( e ) { e.preventDefault(); var button = $( this ); var field_id = $( button ).attr( 'data-field-id' ); var last = $( '.wcmo_field_wrapper_' + field_id ).last().addClass( 'last' ); var wrapper = $( last ).clone().removeClass( 'last' ).insertAfter( $( '.last.wcmo_field_wrapper_' + field_id ) ); // var wrapper = $( '.wcmo_field_wrapper_' + field_id ).last().clone().insertAfter( $( '.wcmo_field_wrapper_' + field_id ) ); $( '.wcmo_field_wrapper_' + field_id ).removeClass( 'last' ); $( wrapper ).find( '.wcmo_field_' + field_id ).addClass( 'xxx' ).val( '' ); var index = $( last ).find( '.wcmo_field' ).attr( 'data-index' ); index ++; $( wrapper ).attr( 'id', field_id + '_' + index + '_field' ); $( wrapper ).find( '.wcmo_field' ).attr( 'name', field_id + '[' + index + ']' ).attr( 'id', field_id + '_' + index ); var original_label = $( '.wcmo_field_wrapper_' + field_id ).first().find( 'label span.wcmo-label-text' ).text(); $( wrapper ).find( '.wcmo_field' ).attr( 'data-index', index ); $( wrapper ).find( 'label span.wcmo-label-text' ).text( original_label + ' [' + index + ']' ); }); // Conditions on registration form var conditions = { init: function() { $( '.woocommerce-form-register').on( 'change keyup', 'input, select', this.update_field ); }, update_field: function() { var updated_field = $( this ); var updated_field_id = $( updated_field ).attr( 'id' ); var updated_value = $( updated_field ).val(); var updated_field_element = $( this ); if( $( updated_field ).hasClass( 'input-checkbox' ) ) { updated_field_element = $( this ).closest( 'label' ); // Set the value according to whether the checkbox is selected updated_value = $( updated_field_element ).find( 'input' ).prop( 'checked' ); } $( '.wcmo_field' ).each( function() { // Look for conditions // For some reason, WooCommerce applies custom attributes to label of checkbox field, not the input field itself var attribute_element = $( this ); if( $( this ).hasClass( 'input-checkbox' ) ) { attribute_element = $( this ).closest( 'label' ); } if( $( attribute_element ).attr( 'data-condition-field' ) == updated_field_id ) { // Do we need Is or Is not? var operator = $( attribute_element ).attr( 'data-condition-operator' ); var display = $( attribute_element ).attr( 'data-condition-display' ); // updated_field is the field that was just updated // conditional_field is the field that has a condition that depends on the value of updated_field conditions.check_field_conditions( updated_field, updated_value, attribute_element, operator, display ); } }); }, check_field_conditions: function( updated_field, updated_value, conditional_field, operator, display ) { var wrapper = $( conditional_field ).closest( '.wcmo_field_wrapper' ); var field = $( wrapper ).find( '.wcmo_field' ); var action; // Check if the updated field is hidden - if so, don't fire any conditions off it if( operator == 'is' ) { if( ( $( conditional_field ).attr( 'data-condition-target' ) == updated_value && display == 'show' ) || ( $( conditional_field ).attr( 'data-condition-target' ) != updated_value && display == 'hide' ) ) { action = 'add'; } else { action = 'remove'; } } else if( operator == 'is-not' ) { if( ( $( conditional_field ).attr( 'data-condition-target' ) != updated_value && display == 'show' ) || ( $( conditional_field ).attr( 'data-condition-target' ) == updated_value && display == 'hide' ) ) { action = 'add'; } else { action = 'remove'; } } if( action == 'add' ) { $( wrapper ).addClass( 'wcmo-show-field' ); } else { $( wrapper ).removeClass( 'wcmo-show-field' ); } $( field ).trigger( 'change' ); } } conditions.init(); $( '.wcmo-file-input' ).change( function() { if ( ! this.files.length ) { $( '#misha_filelist' ).empty(); } else { var hidden_field = $( this ).closest( 'p' ).find( '.wcmo-hidden-file-input' ); var preview_field = $( this ).closest( 'p' ).find( '.wcmo-uploaded-file' ); // Only use one file var file = this.files[0]; $( preview_field ).html( '' ); var formData = new FormData(); formData.append( 'wcmo_upload_file', file ); $.ajax({ url: wcmo_params.ajaxurl + '?action=wcmo_upload_file', type: 'POST', data: formData, contentType: false, enctype: 'multipart/form-data', processData: false, success: function ( response ) { if( response.success ) { $( hidden_field ).val( response.data ); } else { alert( 'File not valid' ); } } }); } } ); })(jQuery);