From a1e40b134f9b6cc9287d0935757deb1a10e98ee7 Mon Sep 17 00:00:00 2001 From: Alexandre Faustino Date: Thu, 12 Dec 2024 10:48:25 +0000 Subject: [PATCH] Update order-script.js --- assets/js/order-script.js | 49 +++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/assets/js/order-script.js b/assets/js/order-script.js index 5965e5354..7bc20432b 100644 --- a/assets/js/order-script.js +++ b/assets/js/order-script.js @@ -3,53 +3,46 @@ jQuery( function( $ ) { $( '#doaction, #doaction2' ).on( 'click', function( e ) { let actionselected = $( this ).attr( "id" ).substr( 2 ); let action = $( 'select[name="' + actionselected + '"]' ).val(); - + if ( $.inArray( action, wpo_wcpdf_ajax.bulk_actions ) !== -1 ) { e.preventDefault(); - let template = action; - let checked = []; - let ubl_output = false; - - // is UBL action - if ( action.indexOf( 'ubl' ) != -1 ) { - template = template.replace( '_ubl', '' ); - ubl_output = true; + let documentType = action; + let outputFormat = 'pdf'; // default format + let checked = []; + + // Determine the format dynamically if appended with an underscore + if ( action.indexOf( '_' ) !== -1 ) { + let parts = action.split( '_' ); + documentType = parts[0]; + outputFormat = parts[1]; } - + $( 'tbody th.check-column input[type="checkbox"]:checked' ).each( function() { checked.push( $( this ).val() ); } ); - + if ( ! checked.length ) { alert( wpo_wcpdf_ajax.select_orders ); return; } - - let partial_url = ''; - let full_url = ''; - - if ( wpo_wcpdf_ajax.ajaxurl.indexOf ("?" ) != -1 ) { - partial_url = wpo_wcpdf_ajax.ajaxurl+'&action=generate_wpo_wcpdf&document_type='+template+'&bulk&_wpnonce='+wpo_wcpdf_ajax.nonce; - } else { - partial_url = wpo_wcpdf_ajax.ajaxurl+'?action=generate_wpo_wcpdf&document_type='+template+'&bulk&_wpnonce='+wpo_wcpdf_ajax.nonce; - } - - // ubl - if ( ubl_output ) { + + let baseUrl = wpo_wcpdf_ajax.ajaxurl; + let separator = baseUrl.includes( '?' ) ? '&' : '?'; + let partial_url = baseUrl + separator + 'action=generate_wpo_wcpdf&document_type=' + documentType + '&bulk&_wpnonce=' + wpo_wcpdf_ajax.nonce; + + // Generate URLs based on format + if ( outputFormat !== 'pdf' ) { $.each( checked, function( i, order_id ) { - full_url = partial_url + '&order_ids='+order_id+'&output=ubl'; + full_url = partial_url + '&order_ids=' + order_id + '&output=' + outputFormat; window.open( full_url, '_blank' ); } ); - - // pdf } else { let order_ids = checked.join( 'x' ); - full_url = partial_url + '&order_ids='+order_ids; + full_url = partial_url + '&order_ids=' + order_ids; window.open( full_url, '_blank' ); } - } } );