Skip to content

Commit

Permalink
Merge pull request #215 from yangyao/shippo
Browse files Browse the repository at this point in the history
Parse tracking number from shippo order notes
  • Loading branch information
yangyao authored Dec 12, 2023
2 parents 76b26da + 8698b45 commit b84f2e1
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 4 deletions.
5 changes: 3 additions & 2 deletions aftership-woocommerce-tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: AfterShip Tracking - All-In-One WooCommerce Order Tracking (Free plan available)
* Plugin URI: http://aftership.com/
* Description: Track orders in one place. shipment tracking, automated notifications, order lookup, branded tracking page, delivery day prediction
* Version: 1.17.6
* Version: 1.17.7
* Author: AfterShip
* Author URI: http://aftership.com
*
Expand All @@ -20,7 +20,7 @@

require_once( 'woo-includes/woo-functions.php' );

define( 'AFTERSHIP_VERSION', '1.17.6' );
define( 'AFTERSHIP_VERSION', '1.17.7' );
define( 'AFTERSHIP_PATH', dirname( __FILE__ ) );
define( 'AFTERSHIP_ASSETS_URL', plugins_url() . '/' . basename( AFTERSHIP_PATH ) );
define( 'AFTERSHIP_SCRIPT_TAGS', 'aftership_script_tags' );
Expand Down Expand Up @@ -216,6 +216,7 @@ public function __construct() {
}
// Get tracking number from order notes created by restful api like royalmail.
add_action( 'woocommerce_rest_insert_order_note', array( $this->actions, 'handle_woocommerce_rest_insert_order_note' ), 10, 2 );
add_action('woocommerce_order_note_added', array( $this->actions, 'handle_woocommerce_insert_order_note' ), 10, 2 );

add_filter( 'rest_shop_order_collection_params', array( $this->actions, 'add_collection_params' ), 10, 1 );
add_filter( 'rest_shop_coupon_collection_params', array( $this->actions, 'add_collection_params' ), 10, 1 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public function create_or_update_settings( WP_REST_Request $data ) {
$options['enable_import_tracking'] = $data['enable_import_tracking'];
}

// save notes to meta, value: 1 or -1
if ( isset( $data['save_notes_to_meta_data'] ) && $data['save_notes_to_meta_data'] ) {
$options['save_notes_to_meta_data'] = $data['save_notes_to_meta_data'];
}

if ( isset( $data['show_orders_actions'] ) && $data['show_orders_actions'] ) {
if ( '' === $this->seek_option_value( $options, 'show_orders_actions' ) ) {
$options['show_orders_actions'] = $data['show_orders_actions'];
Expand Down
85 changes: 84 additions & 1 deletion includes/class-aftership-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1334,8 +1334,47 @@ public function handle_woocommerce_rest_insert_order_note($comment, $request) {
$this->add_tracking_item( $order->get_id(), array( 'tracking_number' => $tracking['tracking_number'], 'slug' => $tracking['slug'] ) );
$order->set_date_modified( current_time( 'mysql' ) );
$order->save();
if ($this->save_notes_to_meta_data_enabled()) {
try {
$order_id = $request['order_id'];
$order_notes = $this->get_order_notes($order_id);
$order = new WC_Order( $order_id );
$order->update_meta_data( '_aftership_order_notes', $order_notes );
$order->save_meta_data();
}catch ( Exception $e) {
return;
}
}
}

/*
* Handle order comment events send by restful api call.
*/
public function handle_woocommerce_insert_order_note($comment_id, $order) {
if ($this->save_notes_to_meta_data_enabled()) {
try {
$order_id = $order->get_id();
$order_notes = $this->get_order_notes($order_id);
$order->update_meta_data( '_aftership_order_notes', $order_notes );
$order->save_meta_data();
}catch ( Exception $e) {
return;
}
}
}

public function save_notes_to_meta_data_enabled() {
$options = get_option( 'aftership_option_name' );
if ( ! $options ) {
return false;
}
$enable = isset( $options['save_notes_to_meta_data'] ) ? $options['save_notes_to_meta_data'] : -1;
if ( $enable !== 1 ) {
return false;
}
return true;
}

/**
* Parse tracking from order note.
*/
Expand All @@ -1354,6 +1393,50 @@ private function get_tracking_from_note ($note) {
$tracking['tracking_number'] = $matches[1];
return $tracking;
}
return null;
$home = get_home_url();
if (strpos($home, "99jersey.com") !== false) {
if (strpos($note, "tracking number") !== false) {
$html = stripslashes($note);
$pattern = '/<a[^>]*>(.*?)<\/a>/i';
preg_match($pattern, $html, $matches);
if (empty($matches[0])) {
return null;
}
$tracking['tracking_number'] = $matches[0];
return $tracking;
}
}
return null;
}

/**
* Get Order Notes
*
* @param $order_id string
* @return array
*/
private function get_order_notes( $order_id ) {
$args = array(
'post_id' => $order_id,
'approve' => 'approve',
'type' => 'order_note',
);

remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
$notes = get_comments( $args );
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );

$order_notes = array();

foreach ( $notes as $note ) {
$order_notes[] = [
"author" => $note->comment_author,
"date_created_gmt" => $note->comment_date_gmt,
"note" => $note->comment_content,
'customer_note' => (bool) get_comment_meta( $note->comment_ID, 'is_customer_note', true ),
];
}

return $order_notes;
}
}
24 changes: 24 additions & 0 deletions includes/class-aftership-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ public function page_init() {
'aftership_setting_section_id'
);

add_settings_field(
'save_notes_to_meta_data',
'',
array( $this, 'save_notes_to_meta_data_callback' ),
'aftership-setting-admin',
'aftership_setting_section_id'
);

add_settings_field(
$this->dom_id_show_order_actions,
'',
Expand Down Expand Up @@ -177,6 +185,12 @@ public function sanitize( $input ) {
}
}

if ( isset( $input['save_notes_to_meta_data'] ) ) {
if ($input['save_notes_to_meta_data'] == 'on' || $input['save_notes_to_meta_data'] === true || intval($input['save_notes_to_meta_data']) === 1 ) {
$new_input['save_notes_to_meta_data'] = 1;
}
}

if ( isset( $input['show_orders_actions'] ) ) {
$new_input['show_orders_actions'] = sanitize_text_field( $input['show_orders_actions'] );
}
Expand Down Expand Up @@ -244,6 +258,16 @@ public function enable_import_tracking_callback() {
);
}

/**
* Call this func before shown on pages.
*/
public function save_notes_to_meta_data_callback() {
printf(
'<div class="auto-as-admin-checkbox-title">Parse tracking from order notes</div><label><input type="checkbox" id="save_notes_to_meta_data" name="aftership_option_name[save_notes_to_meta_data]" %s>Enable</label>',
( isset( $this->options['save_notes_to_meta_data'] ) && 1 === $this->options['save_notes_to_meta_data'] ) ? 'checked="checked"' : ''
);
}

/**
* Call this func before shown on pages.
*/
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Donate link: https://www.aftership.com/
Tags: woocommerce shipping,woocommerce tracking,shipment tracking,order tracking, woocommerce,track order,dhl,ups,usps,fedex,shipping,tracking,order
Requires at least: 2.9
Tested up to: 6.3
Stable tag: 1.17.6
Stable tag: 1.17.7
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down

0 comments on commit b84f2e1

Please sign in to comment.