From be93cf236797e14192950524aca7eed96b8df95e Mon Sep 17 00:00:00 2001 From: Simon Gomes Date: Sat, 8 May 2021 14:25:35 +0600 Subject: [PATCH] Label printing functionality added --- assets/css/admin.css | 5 ++ assets/js/admin.js | 33 ++++++++++++- .../Admin/views/ste-booking-metabox-view.php | 11 ++++- includes/Ajax.php | 48 ++++++++++++++++++- 4 files changed, 93 insertions(+), 4 deletions(-) diff --git a/assets/css/admin.css b/assets/css/admin.css index c8a9cb5..0f4fa4e 100644 --- a/assets/css/admin.css +++ b/assets/css/admin.css @@ -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; } \ No newline at end of file diff --git a/assets/js/admin.js b/assets/js/admin.js index de3b3b7..887b0ca 100644 --- a/assets/js/admin.js +++ b/assets/js/admin.js @@ -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' ); @@ -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); \ No newline at end of file diff --git a/includes/Admin/views/ste-booking-metabox-view.php b/includes/Admin/views/ste-booking-metabox-view.php index e3824b0..dfc4d73 100644 --- a/includes/Admin/views/ste-booking-metabox-view.php +++ b/includes/Admin/views/ste-booking-metabox-view.php @@ -64,7 +64,7 @@

  • - +
  • @@ -97,4 +97,13 @@ + +

    + + +
    + + +
    + diff --git a/includes/Ajax.php b/includes/Ajax.php index dd0e36c..deebc90 100644 --- a/includes/Ajax.php +++ b/includes/Ajax.php @@ -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' ) ); } @@ -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!' ); } @@ -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. *