Skip to content

Commit

Permalink
Label printing functionality added
Browse files Browse the repository at this point in the history
  • Loading branch information
simongomes committed May 8, 2021
1 parent 65e5efe commit be93cf2
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 4 deletions.
5 changes: 5 additions & 0 deletions assets/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@
#ste-metabox-wrap #ste-booking-metabox-message {
display: none;
text-align: center;
}

#ste-metabox-actions {
display: flex;
justify-content: space-between;
}
33 changes: 31 additions & 2 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//! license uri : https://www.gnu.org/licenses/gpl-3.0.html

;(function ($) {
let parcelSubmitButton = $("#submit_ste_ecourier_parcel");
// eCourier Parcel Booking - start
let parcelSubmitButton = $("#submit-ste-ecourier-parcel");
let bookingFormWrap = $( "#ste-metabox-wrap" );
let errorMessage = $( '.error-message' );
let bookingForm = $( '#ste-booking-metabox-form' );
Expand Down Expand Up @@ -67,6 +68,34 @@
}
})
}

});
// eCourier Parcel Booking - end

// eCourier Print Label - start
let labelPrintButton = $("#ste-print-label");

labelPrintButton.on( 'click', function (e) {
e.preventDefault();
labelPrintButton.prop( 'disabled', true );

let labelData = {
tracking : labelPrintButton.val(),
action : 'ste_label_print',
_nonce : STE_ADMIN.nonce,
}

$.post(STE_ADMIN.ajaxurl, labelData, function ( response ) {
if ( ! response.success ) {
errorMessage.text(response.data.message);
} else {
errorMessage.text("");

// On success open the label in a new window.
open(response.data.message);
}
labelPrintButton.prop( 'disabled', false );
});
})
// eCourier Print Label - end

})(jQuery, window);
11 changes: 10 additions & 1 deletion includes/Admin/views/ste-booking-metabox-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<p class="error-message"></p>
</li>
<li class="wide">
<?php submit_button( __( 'Submit', 'ship-to-ecourier' ), 'primary', 'submit_ste_ecourier_parcel', false ); ?>
<?php submit_button( __( 'Submit', 'ship-to-ecourier' ), 'primary', 'submit-ste-ecourier-parcel', false ); ?>
</li>

</ul>
Expand Down Expand Up @@ -97,4 +97,13 @@
</span>
</h4>
</div>

<p class="error-message"></p>

<?php if ( $order_shipped ) { ?>
<div id="ste-metabox-actions">
<button id="ste-print-label" class="button button-primary" value="<?php echo esc_html( $order_shipped->tracking_id ); ?>">Print Label</button>
<button id="ste-cancle-order" class="button button-cancel" value="<?php echo esc_html( $order_shipped->tracking_id ); ?>">Cancle Order</button>
</div>
<?php } ?>
</div>
48 changes: 47 additions & 1 deletion includes/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function __construct() {
add_action( 'wp_ajax_ste_parcel_tracking_form', array( $this, 'handle_parcel_tracker_form_submission' ) );
add_action( 'wp_ajax_nopriv_ste_parcel_tracking_form', array( $this, 'handle_parcel_tracker_form_submission' ) );
add_action( 'wp_ajax_ste_booking_metabox_form', array( $this, 'handle_booking_metabox_form_submission' ) );
add_action( 'wp_ajax_ste_label_print', array( $this, 'handle_label_print' ) );

}

Expand Down Expand Up @@ -91,7 +92,7 @@ public function handle_parcel_tracker_form_submission() {
*/
public function handle_booking_metabox_form_submission() {

if ( ! isset( $_POST['submit_ste_ecourier_parcel'] ) || ! isset( $_POST['_nonce'] ) ) {
if ( ! isset( $_POST['submit-ste-ecourier-parcel'] ) || ! isset( $_POST['_nonce'] ) ) {
die( 'Request is not valid!' );
}

Expand Down Expand Up @@ -177,6 +178,51 @@ public function handle_booking_metabox_form_submission() {
);
}


/**
* Handle label print request for parcel bookings.
*
* @retun void
*/
public function handle_label_print() {
if ( ! isset( $_POST['tracking'] ) || ! isset( $_POST['_nonce'] ) ) {
wp_send_json_error(
array(
'message' => __( 'Request is not valid!', 'ship-to-ecourier' ),
)
);
}

// Block if _nonce field is not available and valid.
check_ajax_referer( 'ste-admin-nonce', '_nonce' );

// Generate label print data to send to eCourier.
$label_data = array(
'tracking' => sanitize_text_field( wp_unslash( $_POST['tracking'] ) ),
);

$ecourier_api_url = $this->ecourier_base_url . '/label-print';

// Send parcel booking request to eCourier.
$response = $this->make_request( $ecourier_api_url, $label_data );

$result = json_decode( $response['body'], true );

if ( ! $result['success'] ) {
wp_send_json_error(
array(
'message' => $result['query_data'],
)
);
}

wp_send_json_success(
array(
'message' => $result['query_data'],
)
);
}

/**
* Make request to eCourier API.
*
Expand Down

0 comments on commit be93cf2

Please sign in to comment.