Skip to content

Commit

Permalink
Merge pull request #2 from kapilpaul/ecourier-customizations
Browse files Browse the repository at this point in the history
Ecourier display city and area in a dropdown
  • Loading branch information
simongomes authored Mar 1, 2023
2 parents a58c10e + 32a21d5 commit 7b6fc03
Show file tree
Hide file tree
Showing 7 changed files with 420 additions and 22 deletions.
59 changes: 57 additions & 2 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( `<option data-thana="${area.thana}" data-post_code="${area.post_code}" value="${area.name.toLowerCase()}">${area.name}</option>` );

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();
Expand All @@ -33,6 +87,7 @@
number_of_item: $( "#number_of_item", bookingFormWrap ).val(),
comments: $( "#comments", bookingFormWrap ).val(),
submit_ste_ecourier_parcel: $( "#submit_ste_ecourier_parcel", bookingFormWrap ).val(),
original_order_number: $( "#original_order_number", bookingFormWrap ).val(),
action: 'ste_booking_metabox_form',
_nonce: STE_ADMIN.nonce,
};
Expand Down Expand Up @@ -112,12 +167,12 @@

let orderData = {
tracking: cancleOrderButton.val(),
original_order_number: $( '#original_order_number' ).val(),
action: 'ste_cancel_parcel_request',
_nonce: STE_ADMIN.nonce,
}

$.post(STE_ADMIN.ajaxurl, orderData, function (response) {
console.log( response );
if (!response.success) {
errorMessage.text(response.data.message);
} else {
Expand All @@ -130,4 +185,4 @@
}
// eCourier Cancel Order - end

})(jQuery, window);
})(jQuery, window);
19 changes: 17 additions & 2 deletions includes/Admin/STE_Metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -84,8 +97,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();
Expand All @@ -99,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 );
}

}
Expand Down
50 changes: 40 additions & 10 deletions includes/Admin/views/ste-booking-metabox-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* @package SendToEcourier\Admin
*/

$recipient_thana = '';
$recipient_post_code = '';

?>

<div id="ste-metabox-wrap">
Expand All @@ -20,37 +23,62 @@
<input class="input-text" type="text" name="recipient_mobile" id="recipient_mobile" placeholder="<?php esc_attr__( 'Recipient Mobile', 'ship-to-ecourier' ); ?>" value="<?php echo esc_attr( $this->shipping_info['recipient_mobile'] ); ?>">
</li>
<li class="wide">
<label for="recipient_city"><?php esc_attr_e( 'Recipient City', 'ship-to-ecourier' ); ?></label>
<input class="input-text" type="text" name="recipient_city" id="recipient_city" placeholder="<?php esc_attr__( 'Recipient City', 'ship-to-ecourier' ); ?>" value="<?php echo esc_attr( $this->shipping_info['recipient_city'] ); ?>">
<label for="recipient_city"><?php esc_attr_e( 'Recipient District', 'ship-to-ecourier' ); ?></label>
<select name="recipient_city" id="recipient_city" class="wc-enhanced-select">
<?php
foreach ( $cities as $city ) :
$city_val = strtolower( $city['value'] );
?>
<option value="<?php echo $city_val; ?>" <?php echo $city_val === $this->shipping_info['recipient_city'] ? 'selected' : false; ?> ><?php echo $city['name']; ?></option>
<?php endforeach; ?>
</select>
</li>
<li class="wide">
<label for="recipient_area"><?php esc_attr_e( 'Recipient Area', 'ship-to-ecourier' ); ?></label>
<input class="input-text" type="text" name="recipient_area" id="recipient_area" placeholder="<?php esc_attr__( 'Recipient Area', 'ship-to-ecourier' ); ?>" value="<?php echo esc_attr( $this->shipping_info['recipient_area'] ); ?>">

<select name="recipient_area" id="recipient_area" class="wc-enhanced-select">
<?php
foreach ( $areas as $area ) :
$area_val = strtolower( $area['name'] );

if ( $area_val === $this->shipping_info['recipient_area'] ) {
$recipient_thana = strtolower( $area['thana'] );
$recipient_post_code = $area['post_code'];
}
?>
<option
data-thana="<?php echo $area['thana']; ?>"
data-post_code="<?php echo $area['post_code']; ?>"
value="<?php echo $area_val; ?>" <?php echo $area_val === $this->shipping_info['recipient_area'] ? 'selected' : false; ?> >
<?php echo $area['name']; ?>
</option>
<?php endforeach; ?>
</select>
</li>
<li class="wide">
<label for="recipient_thana"><?php esc_attr_e( 'Recipient Thana', 'ship-to-ecourier' ); ?></label>
<input class="input-text" type="text" name="recipient_thana" id="recipient_thana" placeholder="<?php esc_attr__( 'Recipient Thana', 'ship-to-ecourier' ); ?>" value="<?php echo esc_attr( $this->shipping_info['recipient_thana'] ); ?>">
<input class="input-text" type="text" name="recipient_thana" id="recipient_thana" placeholder="<?php esc_attr__( 'Recipient Thana', 'ship-to-ecourier' ); ?>" value="<?php echo esc_attr( $recipient_thana ); ?>" readonly>
</li>
<li class="wide">
<label for="recipient_zip"><?php esc_attr_e( 'Recipient Zip', 'ship-to-ecourier' ); ?></label>
<input class="input-text" type="text" name="recipient_zip" id="recipient_zip" placeholder="<?php esc_attr__( 'Recipient Zip', 'ship-to-ecourier' ); ?>" value="<?php echo esc_attr( $this->shipping_info['recipient_zip'] ); ?>">
<input class="input-text" type="text" name="recipient_zip" id="recipient_zip" placeholder="<?php esc_attr__( 'Recipient Zip', 'ship-to-ecourier' ); ?>" value="<?php echo esc_attr( $recipient_post_code ); ?>" readonly>
</li>
<li class="wide">
<label for="recipient_address"><?php esc_attr_e( 'Recipient Address', 'ship-to-ecourier' ); ?></label>
<textarea class="input-text" name="recipient_address" id="recipient_address" placeholder="<?php esc_attr__( 'Recipient Address', 'ship-to-ecourier' ); ?>" required><?php echo esc_html( $this->shipping_info['recipient_address'] ); ?></textarea>
</li>
<li class="wide">
<label for="payment_method"><?php esc_attr_e( 'Payment Method', 'ship-to-ecourier' ); ?></label>
<select name="payment_method" id="payment_method">
<option value="CCRD"><?php esc_html_e( 'Card Payment', 'ship-to-ecourier' ); ?></option>
<select name="payment_method" id="payment_method" class="wc-enhanced-select">
<option value="CCRD" <?php echo 'ccrd' === $this->shipping_info['payment_method'] ? 'selected' : false; ?>><?php esc_html_e( 'Card Payment', 'ship-to-ecourier' ); ?></option>
<option value="COD" <?php echo 'cod' === $this->shipping_info['payment_method'] ? 'selected' : false; ?>><?php esc_html_e( 'Cash On Delivery', 'ship-to-ecourier' ); ?></option>
<option value="MPAY"><?php esc_html_e( 'Mobile Payment', 'ship-to-ecourier' ); ?></option>
<option value="POS"><?php esc_html_e( 'POS', 'ship-to-ecourier' ); ?></option>
<option value="MPAY" <?php echo 'mpay' === $this->shipping_info['payment_method'] ? 'selected' : false; ?>><?php esc_html_e( 'Mobile Payment', 'ship-to-ecourier' ); ?></option>
<option value="POS" <?php echo 'pos' === $this->shipping_info['payment_method'] ? 'selected' : false; ?>><?php esc_html_e( 'POS', 'ship-to-ecourier' ); ?></option>
</select>
</li>
<li class="wide">
<label for="package_code"><?php esc_attr_e( 'Package', 'ship-to-ecourier' ); ?></label>
<select name="package_code" id="package_code">
<select name="package_code" id="package_code" class="wc-enhanced-select">
<?php
foreach ( $this->shipping_info['package_code'] as $package ) {
?>
Expand Down Expand Up @@ -106,4 +134,6 @@
<button id="ste-cancel-order" class="button button-cancel" value="<?php echo esc_html( $order_shipped->tracking_id ); ?>">Cancel Order</button>
</div>
<?php } ?>

<input type="hidden" name="original_order_number" id="original_order_number" value="<?php echo esc_attr( $post->ID ); ?>">
</div>
Loading

0 comments on commit 7b6fc03

Please sign in to comment.