Skip to content

Commit

Permalink
shipped order info stored to database
Browse files Browse the repository at this point in the history
  • Loading branch information
simongomes committed Apr 28, 2021
1 parent d4c59bc commit 9d53dc2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
12 changes: 6 additions & 6 deletions includes/Admin/STE_Metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@ class STE_Metabox {
* @return void;
*/
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'ste_register_metabox' ) );
add_action( 'add_meta_boxes', array( $this, 'register_metabox' ) );
}

/**
* The method handles the metabox registration.
*
* @return void;
*/
public function ste_register_metabox() {
add_meta_box( 'ste-booking-metabox', __( 'Ship To eCourier', 'send-to-ecourier' ), array( $this, 'ste_metabox_view_handler' ), 'shop_order', 'side' );
public function register_metabox() {
add_meta_box( 'ste-booking-metabox', __( 'Ship To eCourier', 'send-to-ecourier' ), array( $this, 'metabox_view_handler' ), 'shop_order', 'side' );
}

/**
* The method is a handler for STE_Metabox.
*
* @return void
*/
public function ste_metabox_view_handler() {
public function metabox_view_handler() {
global $theorder, $post;

if ( ! is_object( $theorder ) ) {
Expand All @@ -59,7 +59,7 @@ public function ste_metabox_view_handler() {
wp_enqueue_script( 'ste-admin-script' );

// Set all necessary Shipping Information.
$this->ste_set_shipping_info( $theorder );
$this->set_shipping_info( $theorder );


// Load the parcel booking form/view.
Expand All @@ -76,7 +76,7 @@ public function ste_metabox_view_handler() {
*
* @return void
*/
public function ste_set_shipping_info( $order ) {
public function set_shipping_info( $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();
Expand Down
17 changes: 17 additions & 0 deletions includes/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,23 @@ public function handle_booking_metabox_form_submission() {
$result = json_decode( $response['body'], true );

if ( $result['success'] ) {
// Insert order shipped record to ste_shipped_orders table.
$insert = ste_insert_shipped_order(
array(
'order_id' => $parcel_data['product_id'],
'tracking_id' => $result['ID'],
)
);

// If WP_Error send error message back to admin panel.
if ( is_wp_error( $insert ) ) {
wp_send_json_error(
array(
'message' => $insert->get_error_message(),
)
);
}

// Get the order to update the order status.
$order = new \WC_Order( $parcel_data['product_id'] );
$order->update_status( 'shipped' );
Expand Down
15 changes: 8 additions & 7 deletions includes/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ public function create_shipped_orders_table() {
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . STE_TABLE_PREFIX . 'shipped_orders';

$schema = "CREATE TABLE `$table_name` (
`ID` bigint(20) UNSIGNED NOT NULL,
`order_id` bigint(20) UNSIGNED NOT NULL,
`tracking_id` varchar(191) COLLATE utf8mb4_unicode_520_ci NOT NULL,
`created_by` bigint(20) UNSIGNED NOT NULL,
`created_at` timestamp NOT NULL
) $charset_collate;";
$schema = "CREATE TABLE `{$table_name}` (
`ID` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`order_id` BIGINT(20) UNSIGNED NOT NULL,
`tracking_id` VARCHAR(191) NOT NULL,
`created_by` BIGINT(20) UNSIGNED NOT NULL,
`created_at` TIMESTAMP NOT NULL,
PRIMARY KEY (`ID`)
) $charset_collate;";

if ( ! function_exists( 'dbDelta' ) ) {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
Expand Down
34 changes: 34 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,37 @@ function ste_get_ecourier_packages() {

return $packages;
}

/**
* Insert successfully shipped order information to database.
*
* @param array $args shipped order information.
*
* @return bool|\WP_Error
*/
function ste_insert_shipped_order( $args = array() ) {
global $wpdb;

$table_name = $wpdb->prefix . STE_TABLE_PREFIX . 'shipped_orders';

$defaults = array(
'order_id' => '',
'tracking_id' => '',
'created_by' => get_current_user_id(),
'created_at' => current_time( 'mysql' ),
);

$data = wp_parse_args( $args, $defaults );

$inserted = $wpdb->insert(
$table_name,
$data,
array( '%d', '%s', '%d', '%s' )
); // db call ok; no-cache ok.

if ( ! $inserted ) {
return new \WP_Error( 'failed-to-insert-shipped-order', __( 'Failed to insert shipped order information!', 'ship-to-ecourier' ) );
}

return true;
}

0 comments on commit 9d53dc2

Please sign in to comment.