Skip to content

Commit

Permalink
Send To eCourier parcel form data created
Browse files Browse the repository at this point in the history
  • Loading branch information
simongomes committed Apr 26, 2021
1 parent 60cbd79 commit 1b12f26
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
5 changes: 5 additions & 0 deletions includes/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function __construct() {
$this->dispatch_actions( $settings );

new Admin\Menu( $settings );

// Check if the WooCommerse plugin is active and fire the STE_Metabox class to add the custom metabox.
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ) {
new Admin\STE_Metabox();
}
}

/**
Expand Down
93 changes: 93 additions & 0 deletions includes/Admin/STE_Metabox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php // phpcs:ignore
/**
* This file contains class that handles the custom Ship To eCourier Metabox.
*
* @package ShipToEcourier
*/

namespace ShipToEcourier\Admin;

if ( ! class_exists( 'STE_Metabox' ) ) {
/**
* Class STE_Metabox
*
* STE_Metabox registers and handles the custom Ship To eCourier metabox to send parcel booking request to eCourier.
*
* @package ShipToEcourier\Admin
*/
class STE_Metabox {

/**
* Hold all shipping information for eCourier.
*
* @var array
*/
private $shipping_info = [];

/**
* STE_Metabox constructor, fires action to add the metabox.
*
* @return void;
*/
public function __construct() {
add_action( 'add_meta_boxes', [ $this, 'ste_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_handler' ), 'shop_order', 'side' );
}

/**
* The method is a handler for STE_Metabox.
*
* @return void
*/
public function ste_metabox_handler() {
global $theorder, $post;
if ( ! is_object( $theorder ) ) {
$theorder = wc_get_order( $post->ID );
}

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

if ( ! file_exists( __DIR__ . '/views/ste-metabox-view.php' ) ) {
return;
}
include __DIR__ . '/views/ste-metabox-view.php';
}

/**
* Set the shipping info to $shipping_info variable to access from the view.
*
* @param mixed $order will hold the WooCommerce Order object.
*
* @return void
*/
public function ste_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();
$this->shipping_info['recipient_area'] = '';
$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();
$this->shipping_info['product_id'] = $order->get_order_number();
$this->shipping_info['product_price'] = $order->get_total();
$this->shipping_info['number_of_item'] = $order->get_item_count();
$this->shipping_info['payment_method'] = $order->get_payment_method();
$this->shipping_info['package_code'] = ste_get_ecourier_packages();
$this->shipping_info['comments'] = '';

foreach ( $order->get_items() as $item ) {
$this->shipping_info['comments'] .= $item->get_name() . ' x' . $item->get_quantity() . PHP_EOL;
}
}

}
}
18 changes: 18 additions & 0 deletions includes/Admin/views/ste-metabox-view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* This file contains the view for STE_Metabox form.
*
* @package SendToEcourier\Admin
*/
?>

<div id="ste-metabox-wrap">
<form action="#" id="ste-metabox-form">
<ul class="ste_metabox submitbox">
<li class="wide">
<label for="recipient_name">Recipient Name</label>
<input class="input-text" type="text" name="recipient_name" id="recipient_name" placeholder="Recipient Name" value="<?php echo esc_attr( $this->shipping_info['recipient_name'] ); ?>">
</li>
</ul>
</form>
</div>
31 changes: 31 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,34 @@ function ste_get_settings() {

return $settings;
}

/**
* Get eCourier packages for the connected account.
*
* @return array
*/
function ste_get_ecourier_packages() {

// Get eCourier API configs.
$settings = ste_get_settings();

// Get eCourier API URL (Staging/Live).
$ecourier_api_url = 'live' === $settings['api_environment'] ? STE_API_BASE_URL_LIVE . '/packages' : STE_API_BASE_URL_STAGING . '/packages';

// Send request to eCourier to fetch the active packages.
$response = wp_remote_post(
$ecourier_api_url,
array(
'method' => 'POST',
'headers' => array(
'USER-ID' => $settings['user_id'],
'API-KEY' => $settings['api_key'],
'API-SECRET' => $settings['api_secret'],
),
)
);

$packages = json_decode( $response['body'] );

return $packages;
}

0 comments on commit 1b12f26

Please sign in to comment.