From 78cbdd6ebf78b02567f6bbaea897ff2de60a36f0 Mon Sep 17 00:00:00 2001 From: Kapil Paul Date: Wed, 22 Feb 2023 13:23:21 +0600 Subject: [PATCH 1/6] Update admin ship to ecourier metabox with area and city api --- includes/Admin/STE_Metabox.php | 4 +- .../Admin/views/ste-booking-metabox-view.php | 41 ++++- includes/Ecourier_Handler.php | 147 ++++++++++++++++++ ship-to-ecourier.php | 46 +++++- 4 files changed, 228 insertions(+), 10 deletions(-) create mode 100644 includes/Ecourier_Handler.php diff --git a/includes/Admin/STE_Metabox.php b/includes/Admin/STE_Metabox.php index 64945c3..1f19fcc 100644 --- a/includes/Admin/STE_Metabox.php +++ b/includes/Admin/STE_Metabox.php @@ -84,8 +84,8 @@ public function metabox_view_handler() { public function set_shipping_info( \WC_Order $order ) { $this->shipping_info['recipient_name'] = '' !== trim( $order->get_formatted_shipping_full_name() ) ? $order->get_formatted_shipping_full_name() : $order->get_formatted_billing_full_name(); $this->shipping_info['recipient_mobile'] = $order->get_billing_phone(); - $this->shipping_info['recipient_city'] = '' !== trim( $order->get_shipping_city() ) ? $order->get_shipping_city() : $order->get_billing_city(); - $this->shipping_info['recipient_area'] = ''; + $this->shipping_info['recipient_city'] = '' !== trim( $order->get_shipping_state() ) ? strtolower( $order->get_shipping_state() ) : strtolower( $order->get_billing_state() ); + $this->shipping_info['recipient_area'] = '' !== trim( $order->get_shipping_city() ) ? strtolower( $order->get_shipping_city() ) : strtolower( $order->get_billing_city() ); $this->shipping_info['recipient_thana'] = ''; $this->shipping_info['recipient_zip'] = '' !== trim( $order->get_shipping_postcode() ) ? $order->get_shipping_postcode() : $order->get_billing_postcode(); $this->shipping_info['recipient_address'] = '' !== trim( $order->get_shipping_address_1() ) ? $order->get_shipping_address_1() . '' . $order->get_shipping_address_2() : $order->get_billing_address_1() . ' ' . $order->get_billing_address_2(); diff --git a/includes/Admin/views/ste-booking-metabox-view.php b/includes/Admin/views/ste-booking-metabox-view.php index 7bcc477..fabe5ce 100644 --- a/includes/Admin/views/ste-booking-metabox-view.php +++ b/includes/Admin/views/ste-booking-metabox-view.php @@ -5,6 +5,9 @@ * @package SendToEcourier\Admin */ +$recipient_thana = ''; +$recipient_post_code = ''; + ?>
@@ -20,20 +23,44 @@
  • - - + +
  • - + +
  • - +
  • - +
  • @@ -41,7 +68,7 @@
  • - @@ -50,7 +77,7 @@
  • - shipping_info['package_code'] as $package ) { ?> diff --git a/includes/Ecourier_Handler.php b/includes/Ecourier_Handler.php new file mode 100644 index 0000000..30ab4f1 --- /dev/null +++ b/includes/Ecourier_Handler.php @@ -0,0 +1,147 @@ +set_base_url(); + } + + /** + * Set base url based on settings. + * + * @return void + */ + public function set_base_url() { + // Get eCourier API configs. + $settings = ste_get_settings(); + + // Get eCourier API URL (Staging/Live). + $this->base_url = 'live' === $settings['api_environment'] ? STE_API_BASE_URL_LIVE : STE_API_BASE_URL_STAGING; + } + + /** + * Get header for ecourier api. + * + * @return array + */ + public function get_headers() { + // Get eCourier API configs. + $settings = ste_get_settings(); + + return [ + 'USER-ID' => $settings['user_id'], + 'API-KEY' => $settings['api_key'], + 'API-SECRET' => $settings['api_secret'], + 'Content-Type' => 'application/json', + ]; + } + + /** + * Get City list. + * + * @return \WP_Error|array + */ + public function get_city_list() { + $cities = get_transient( self::CITY_LIST_CACHE_KEY ); + + if ( $cities ) { + return $cities; + } + + $url = $this->base_url . '/city-list'; + + $response = wp_remote_post( + $url, + array( + 'method' => 'POST', + 'headers' => $this->get_headers(), + ) + ); + + if ( is_wp_error( $response ) ) { + return new \WP_Error( 'ste_get_city_error', __( 'Error in getting city list', 'ship-to-ecourier' ), [ 'status' => 500 ] ); + } + + $body = wp_remote_retrieve_body( $response ); + + $cities = json_decode( $body, true ); + + //keeping data in cache. Expire in 24 hours. + set_transient( self::CITY_LIST_CACHE_KEY, $cities, 86400 ); + + return $cities; + } + + /** + * Get area by district. + * + * @param string $district + * + * @return array|\WP_Error + */ + public function get_area_by_district( $district ) { + $cache_key = $district . self::AREA_LIST_CACHE_KEY; + + $areas_data = get_transient( $cache_key ); + + if ( $areas_data ) { + return $areas_data; + } + + $url = $this->base_url . '/area-by-district'; + + $response = wp_remote_post( + $url, + array( + 'headers' => $this->get_headers(), + 'body' => wp_json_encode( [ 'district' => $district ] ), + ) + ); + + if ( is_wp_error( $response ) ) { + return new \WP_Error( 'ste_get_area_error', __( 'Error in getting Area list', 'ship-to-ecourier' ), [ 'status' => 500 ] ); + } + + $body = wp_remote_retrieve_body( $response ); + + $areas = json_decode( $body, true ); + + if ( isset( $areas['success'] ) && false === $areas['success'] ) { + return new \WP_Error( + 'ste_get_area_error', + __( 'Error in getting Area list', 'ship-to-ecourier' ), + [ + 'status' => 500, + 'data' => $areas['errors'] ? $areas['errors'] : $areas['data'] + ] + ); + } + + $areas_data = $areas['data']; + + //keeping data in cache. Expire in 24 hours. + set_transient( $cache_key, $areas_data, 86400 ); + + return $areas_data; + } +} diff --git a/ship-to-ecourier.php b/ship-to-ecourier.php index 6d90d32..2409496 100644 --- a/ship-to-ecourier.php +++ b/ship-to-ecourier.php @@ -66,6 +66,13 @@ final class Ship_To_Ecourier { */ const VERSION = '1.1.0'; + /** + * Holds various class instances. + * + * @var array + */ + private $container = []; + /** * Ship_To_Ecourier constructor. * @@ -77,6 +84,8 @@ public function __construct() { register_activation_hook( __FILE__, array( $this, 'activate' ) ); add_action( 'plugins_loaded', array( $this, 'init_plugin' ) ); + + add_action( 'init', [ $this, 'init_classes' ] ); } /** @@ -85,7 +94,7 @@ public function __construct() { * @return \Ship_To_Ecourier */ public static function init() { - $instance = false; + static $instance = false; if ( ! $instance ) { $instance = new self(); @@ -94,6 +103,32 @@ public static function init() { return $instance; } + /** + * Magic getter to bypass referencing plugin. + * + * @param mixed $prop Properties to find. + * + * @return mixed + */ + public function __get( $prop ) { + if ( array_key_exists( $prop, $this->container ) ) { + return $this->container[ $prop ]; + } + + return $this->{$prop}; + } + + /** + * Magic isset to bypass referencing plugin. + * + * @param mixed $prop Properties to find. + * + * @return mixed + */ + public function __isset( $prop ) { + return isset( $this->{$prop} ) || isset( $this->container[ $prop ] ); + } + /** * Defines all necessary constants for the plugin. * @@ -136,6 +171,15 @@ public function init_plugin() { } } + /** + * Initialize classes + * + * @return void + */ + public function init_classes() { + $this->container['ecourier'] = new ShipToEcourier\Ecourier_Handler(); + } + /** * Necessary setup on plugin activation. * From 6252f08971d50e37f112cb6ecab968f7293e6ada Mon Sep 17 00:00:00 2001 From: Kapil Paul Date: Wed, 22 Feb 2023 14:49:09 +0600 Subject: [PATCH 2/6] ajax call for getting areas based on city --- assets/js/admin.js | 57 ++++++++++++++++++- .../Admin/views/ste-booking-metabox-view.php | 7 ++- includes/Ajax.php | 20 +++++++ includes/Ecourier_Handler.php | 18 +++--- 4 files changed, 90 insertions(+), 12 deletions(-) diff --git a/assets/js/admin.js b/assets/js/admin.js index 0b56dab..4c4e01e 100644 --- a/assets/js/admin.js +++ b/assets/js/admin.js @@ -13,6 +13,60 @@ let errorMessage = $( '.error-message' ); let bookingForm = $( '#ste-booking-metabox-form' ); let bookingMetaBoxMessage = $( '#ste-booking-metabox-message' ); + let recipientCity = $( '#recipient_city' ); + let recipientArea = $( '#recipient_area' ); + let recipientThana = $( '#recipient_thana' ); + let recipientZip = $( '#recipient_zip' ); + + /** + * Setting area on change of city/district. + */ + recipientCity.on( 'change', function ( event ) { + let data = { + action: 'ste_get_area_by_district', + district: this.value, + _nonce: STE_ADMIN.nonce, + }; + + recipientArea.prop( 'disabled', true ); + + $.post( STE_ADMIN.ajaxurl, data, function ( response ) { + recipientArea.empty(); + recipientThana.val( '' ); + recipientZip.val( '' ); + + if ( ! response.success ) { + errorMessage.text( response.data ); + parcelSubmitButton.prop( 'disabled', true ); + return; + } + + errorMessage.text( '' ); + + recipientArea.prop( 'disabled', false ); + + let areas = response.data; + + areas.map( function ( area, index ) { + recipientArea.append( `` ); + + if ( index === 0 ) { + recipientThana.val( area.thana ); + recipientZip.val( area.post_code ); + } + } ); + } ); + } ); + + /** + * Setting thana and post code on change of area. + */ + recipientArea.on( 'change', function ( event ) { + let optionSelected = $( 'option:selected', this ); + + recipientThana.val( optionSelected.data( 'thana' ) ); + recipientZip.val( optionSelected.data( 'post_code' ) ); + } ); parcelSubmitButton.on("click", function (e) { e.preventDefault(); @@ -117,7 +171,6 @@ } $.post(STE_ADMIN.ajaxurl, orderData, function (response) { - console.log( response ); if (!response.success) { errorMessage.text(response.data.message); } else { @@ -130,4 +183,4 @@ } // eCourier Cancel Order - end -})(jQuery, window); \ No newline at end of file +})(jQuery, window); diff --git a/includes/Admin/views/ste-booking-metabox-view.php b/includes/Admin/views/ste-booking-metabox-view.php index fabe5ce..58eecea 100644 --- a/includes/Admin/views/ste-booking-metabox-view.php +++ b/includes/Admin/views/ste-booking-metabox-view.php @@ -50,7 +50,12 @@ $recipient_post_code = $area['post_code']; } ?> - +
  • diff --git a/includes/Ajax.php b/includes/Ajax.php index 313c1c6..bf3a169 100644 --- a/includes/Ajax.php +++ b/includes/Ajax.php @@ -43,6 +43,7 @@ public function __construct() { // Set eCourier base url. $this->ecourier_base_url = 'live' === $this->settings['api_environment'] ? STE_API_BASE_URL_LIVE : STE_API_BASE_URL_STAGING; + add_action( 'wp_ajax_ste_get_area_by_district', array( $this, 'handle_ste_get_area_by_district' ) ); 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' ) ); @@ -51,6 +52,25 @@ public function __construct() { } + public function handle_ste_get_area_by_district() { + if ( ! isset( $_POST['_nonce'] ) || empty( $_POST['district'] ) ) { + wp_send_json_error( __( 'Something went wrong here!', 'ship-to-ecourier' ) ); + wp_die(); + } + + // Block if _nonce field is not available and valid. + check_ajax_referer( 'ste-admin-nonce', '_nonce' ); + + $areas = ship_to_ecourier()->ecourier->get_area_by_district( sanitize_text_field( $_POST['district'] ) ); + + if ( is_wp_error( $areas ) ) { + wp_send_json_error( $areas->get_error_message() ); + wp_die(); + } + + wp_send_json_success( $areas ); + } + /** * Handle the tracking form submission. Get parcel status from eCourier and return back to front end. * diff --git a/includes/Ecourier_Handler.php b/includes/Ecourier_Handler.php index 30ab4f1..a3225a1 100644 --- a/includes/Ecourier_Handler.php +++ b/includes/Ecourier_Handler.php @@ -86,8 +86,8 @@ public function get_city_list() { $cities = json_decode( $body, true ); - //keeping data in cache. Expire in 24 hours. - set_transient( self::CITY_LIST_CACHE_KEY, $cities, 86400 ); + //keeping data in cache. Expire in 48 hours. + set_transient( self::CITY_LIST_CACHE_KEY, $cities, 172800 ); return $cities; } @@ -95,7 +95,7 @@ public function get_city_list() { /** * Get area by district. * - * @param string $district + * @param string $district District name. * * @return array|\WP_Error */ @@ -112,14 +112,14 @@ public function get_area_by_district( $district ) { $response = wp_remote_post( $url, - array( + [ 'headers' => $this->get_headers(), - 'body' => wp_json_encode( [ 'district' => $district ] ), - ) + 'body' => wp_json_encode( [ 'district' => str_replace( "\'", "'", $district ) ] ), + ] ); if ( is_wp_error( $response ) ) { - return new \WP_Error( 'ste_get_area_error', __( 'Error in getting Area list', 'ship-to-ecourier' ), [ 'status' => 500 ] ); + return new \WP_Error( 'ste_get_area_error', __( 'Error in getting Area list', 'ship-to-ecourier' ), [ 'status' => 500, 'data' => $response->get_error_data() ] ); } $body = wp_remote_retrieve_body( $response ); @@ -139,8 +139,8 @@ public function get_area_by_district( $district ) { $areas_data = $areas['data']; - //keeping data in cache. Expire in 24 hours. - set_transient( $cache_key, $areas_data, 86400 ); + //keeping data in cache. Expire in 48 hours. + set_transient( $cache_key, $areas_data, 172800 ); return $areas_data; } From 1e40bc2a3eec42f717ec138fe64cbf821823ee0b Mon Sep 17 00:00:00 2001 From: Kapil Paul Date: Fri, 24 Feb 2023 11:27:42 +0600 Subject: [PATCH 3/6] add error handling for ecourier --- includes/Ajax.php | 24 ++++++++++++++++++++++++ includes/functions.php | 17 ++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/includes/Ajax.php b/includes/Ajax.php index bf3a169..81a6ebe 100644 --- a/includes/Ajax.php +++ b/includes/Ajax.php @@ -90,6 +90,14 @@ public function handle_parcel_tracker_form_submission() { // Make request to eCourier API. $response = $this->make_request( $ecourier_api_url, array( 'ecr' => $tracking_code ) ); + if ( is_wp_error( $response ) ) { + wp_send_json_error( + array( + 'message' => $response->get_error_data(), + ) + ); + } + // Send response to front-end. wp_send_json_success( array( @@ -228,6 +236,14 @@ public function handle_label_print() { // Send parcel label print request to eCourier. $response = $this->make_request( $ecourier_api_url, $label_data ); + if ( is_wp_error( $response ) ) { + wp_send_json_error( + array( + 'message' => $response->get_error_data(), + ) + ); + } + $result = json_decode( $response['body'], true ); if ( ! $result['success'] ) { @@ -274,6 +290,14 @@ public function handle_parcel_cancel_request() { // Send parcel cancel request to eCourier. $response = $this->make_request( $ecourier_api_url, $label_data ); + if ( is_wp_error( $response ) ) { + wp_send_json_error( + array( + 'message' => $response->get_error_data(), + ) + ); + } + $result = json_decode( $response['body'], true ); if ( ! 'true' === $result['success'] ) { diff --git a/includes/functions.php b/includes/functions.php index 5f74781..7780015 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -42,10 +42,18 @@ function ste_get_settings() { /** * Get eCourier packages for the connected account. * - * @return array + * @return array|\WP_Error */ function ste_get_ecourier_packages() { + $cache_key = 'ste_packages_list'; + + $packages = get_transient( $cache_key ); + + if ( $packages ) { + return $packages; + } + // Get eCourier API configs. $settings = ste_get_settings(); @@ -65,8 +73,15 @@ function ste_get_ecourier_packages() { ) ); + if ( is_wp_error( $response ) ) { + return new WP_Error( 'ste_package_fetch_error', 'Error in getting packages', [ 'status' => 500 ] ); + } + $packages = json_decode( $response['body'] ); + //keeping data in cache. Expire in 48 hours. + set_transient( $cache_key, $packages, 172800 ); + return $packages; } From 62de163e3cef1a200a43d10568ff00bdd65cb8a0 Mon Sep 17 00:00:00 2001 From: Kapil Paul Date: Fri, 24 Feb 2023 15:01:07 +0600 Subject: [PATCH 4/6] add error handling for ecourier --- includes/Admin/STE_Metabox.php | 13 +++++++++++++ includes/Admin/views/ste-booking-metabox-view.php | 4 ---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/includes/Admin/STE_Metabox.php b/includes/Admin/STE_Metabox.php index 1f19fcc..9071213 100644 --- a/includes/Admin/STE_Metabox.php +++ b/includes/Admin/STE_Metabox.php @@ -63,6 +63,19 @@ public function metabox_view_handler() { if ( ! $order_shipped ) { // Set all necessary Shipping Information. $this->set_shipping_info( $theorder ); + + $cities = ship_to_ecourier()->ecourier->get_city_list(); + + if ( is_wp_error( $cities ) ) { + $cities = []; + } + + $areas = ship_to_ecourier()->ecourier->get_area_by_district( $this->shipping_info['recipient_city'] ); + + if ( is_wp_error( $cities ) ) { + $areas = []; + } + } else { $order_shipped->user = get_user_by( 'ID', $order_shipped->created_by )->display_name; } diff --git a/includes/Admin/views/ste-booking-metabox-view.php b/includes/Admin/views/ste-booking-metabox-view.php index 58eecea..89c4ab5 100644 --- a/includes/Admin/views/ste-booking-metabox-view.php +++ b/includes/Admin/views/ste-booking-metabox-view.php @@ -25,8 +25,6 @@
  • +
  • diff --git a/includes/Ajax.php b/includes/Ajax.php index 81a6ebe..3454f4a 100644 --- a/includes/Ajax.php +++ b/includes/Ajax.php @@ -156,9 +156,9 @@ public function handle_booking_metabox_form_submission() { $parcel_data = array( 'recipient_name' => sanitize_text_field( wp_unslash( $_POST['recipient_name'] ) ), 'recipient_mobile' => sanitize_text_field( wp_unslash( $_POST['recipient_mobile'] ) ), - 'recipient_city' => sanitize_text_field( wp_unslash( $_POST['recipient_city'] ) ), - 'recipient_area' => sanitize_text_field( wp_unslash( $_POST['recipient_area'] ) ), - 'recipient_thana' => sanitize_text_field( wp_unslash( $_POST['recipient_thana'] ) ), + 'recipient_city' => ucwords( sanitize_text_field( wp_unslash( $_POST['recipient_city'] ) ) ), + 'recipient_area' => ucwords( sanitize_text_field( wp_unslash( $_POST['recipient_area'] ) ) ), + 'recipient_thana' => ucwords( sanitize_text_field( wp_unslash( $_POST['recipient_thana'] ) ) ), 'recipient_zip' => sanitize_text_field( wp_unslash( $_POST['recipient_zip'] ) ), 'recipient_address' => sanitize_text_field( wp_unslash( $_POST['recipient_address'] ) ), 'payment_method' => sanitize_text_field( wp_unslash( $_POST['payment_method'] ) ), @@ -174,6 +174,14 @@ public function handle_booking_metabox_form_submission() { // Send parcel booking request to eCourier. $response = $this->make_request( $ecourier_api_url, $parcel_data ); + if ( is_wp_error( $response ) ) { + wp_send_json_error( + array( + 'message' => $response->get_error_data(), + ) + ); + } + $result = json_decode( $response['body'], true ); if ( $result['success'] ) { @@ -194,8 +202,14 @@ public function handle_booking_metabox_form_submission() { ); } - // Get the order to update the order status. - $order = new \WC_Order( $parcel_data['product_id'] ); + /** + * Get the order to update the order status. + * + * using original order number here because sometimes the order + * number might be modified through `woocommerce_order_number` filter + * by third party plugin. + */ + $order = new \WC_Order( sanitize_text_field( wp_unslash( $_POST['original_order_number'] ) ) ); $order->update_status( 'shipped' ); } From 32a21d5db1015c13ccea21892f91ddd5a5d9cae9 Mon Sep 17 00:00:00 2001 From: Kapil Paul Date: Sat, 25 Feb 2023 15:44:12 +0600 Subject: [PATCH 6/6] add order notes when ship and add filter --- assets/js/admin.js | 1 + includes/Admin/STE_Metabox.php | 2 ++ .../Admin/views/ste-booking-metabox-view.php | 9 ++--- includes/Ajax.php | 36 ++++++++++++++++++- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/assets/js/admin.js b/assets/js/admin.js index 361a09d..f0b5393 100644 --- a/assets/js/admin.js +++ b/assets/js/admin.js @@ -167,6 +167,7 @@ let orderData = { tracking: cancleOrderButton.val(), + original_order_number: $( '#original_order_number' ).val(), action: 'ste_cancel_parcel_request', _nonce: STE_ADMIN.nonce, } diff --git a/includes/Admin/STE_Metabox.php b/includes/Admin/STE_Metabox.php index 9071213..8de6611 100644 --- a/includes/Admin/STE_Metabox.php +++ b/includes/Admin/STE_Metabox.php @@ -112,6 +112,8 @@ public function set_shipping_info( \WC_Order $order ) { foreach ( $order->get_items() as $item ) { $this->shipping_info['comments'] .= $item->get_name() . ' x' . $item->get_quantity() . PHP_EOL; } + + $this->shipping_info = apply_filters( 'ste_set_shipping_info', $this->shipping_info, $order ); } } diff --git a/includes/Admin/views/ste-booking-metabox-view.php b/includes/Admin/views/ste-booking-metabox-view.php index e23c97e..874bda0 100644 --- a/includes/Admin/views/ste-booking-metabox-view.php +++ b/includes/Admin/views/ste-booking-metabox-view.php @@ -70,10 +70,10 @@
  • @@ -100,7 +100,6 @@ -
  • @@ -135,4 +134,6 @@
    + + diff --git a/includes/Ajax.php b/includes/Ajax.php index 3454f4a..e903a08 100644 --- a/includes/Ajax.php +++ b/includes/Ajax.php @@ -150,6 +150,7 @@ public function handle_booking_metabox_form_submission() { 'message' => __( 'All fields are required!', 'ship-to-ecourier' ), ) ); + return; } // Generate parcel booking data to send to eCourier. @@ -180,6 +181,7 @@ public function handle_booking_metabox_form_submission() { 'message' => $response->get_error_data(), ) ); + return; } $result = json_decode( $response['body'], true ); @@ -200,6 +202,18 @@ public function handle_booking_metabox_form_submission() { 'message' => $insert->get_error_message(), ) ); + return; + } + + $user = get_user_by( 'id', get_current_user_id() ); + + $order_shipped_note = ''; + + if ( $user ) { + $order_shipped_note = sprintf( + __( 'Order shipped to Ecourier by %s', 'ship-to-ecourier' ), + $user->display_name + ); } /** @@ -210,6 +224,7 @@ public function handle_booking_metabox_form_submission() { * by third party plugin. */ $order = new \WC_Order( sanitize_text_field( wp_unslash( $_POST['original_order_number'] ) ) ); + ! empty( $order_shipped_note ) ? $order->add_order_note( $order_shipped_note ) : null; $order->update_status( 'shipped' ); } @@ -293,10 +308,12 @@ public function handle_parcel_cancel_request() { // Block if _nonce field is not available and valid. check_ajax_referer( 'ste-admin-nonce', '_nonce' ); + $user = get_user_by( 'id', get_current_user_id() ); + // Generate label print data to send to eCourier. $label_data = array( 'tracking' => sanitize_text_field( wp_unslash( $_POST['tracking'] ) ), - 'comment' => __( 'Please cancle the parcel.', 'ship-to-ecourier' ), + 'comment' => __( 'Please cancel the parcel. Requested by ' . $user->display_name , 'ship-to-ecourier' ), ); $ecourier_api_url = $this->ecourier_base_url . '/cancel-order'; @@ -333,6 +350,23 @@ public function handle_parcel_cancel_request() { } } + $order = wc_get_order( wp_unslash( $_POST['original_order_number'] ) ); + + if ( $order instanceof \WC_Order) { + $order_cancel_note = ''; + + if ( $user ) { + $order_cancel_note = sprintf( + __( 'Shipping cancelled at Ecourier by %s', 'ship-to-ecourier' ), + $user->display_name + ); + } + + ! empty( $order_cancel_note ) ? $order->add_order_note( $order_cancel_note ) : null; + + $order->update_status( 'processing' ); + } + wp_send_json_success( array( 'message' => $result['message'],