Skip to content

Commit

Permalink
Update order-script.js
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmigf committed Dec 12, 2024
1 parent 8f56b61 commit a1e40b1
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions assets/js/order-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}

}
} );

Expand Down

0 comments on commit a1e40b1

Please sign in to comment.