Skip to content

Commit d055b7d

Browse files
committed
Update WCST plugin and allow it to take care of tracking info emails
1 parent ed893cc commit d055b7d

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
if ( ! class_exists( 'WC_Connect_Extension_Compatibility' ) ) {
4+
class WC_Connect_Extension_Compatibility {
5+
/**
6+
* Function called when a new tracking number is added to the order
7+
*
8+
* @param $order_id - order ID
9+
* @param $carrier_id - carrier ID, as returned on the label objects returned by the server
10+
* @param $tracking_number - tracking number string
11+
*/
12+
public static function on_new_tracking_number( $order_id, $carrier_id, $tracking_number ) {
13+
//call WooCommerce Shipment Tracking if it's installed
14+
if ( function_exists( 'wc_st_add_tracking_number' ) ) {
15+
//note: the only carrier ID we use at the moment is 'usps', which is the same in WC_ST, but this might require a mapping
16+
wc_st_add_tracking_number( $order_id, $tracking_number, $carrier_id );
17+
}
18+
}
19+
20+
/**
21+
* Checks if WooCommerce Services should email the tracking details, or if another extension is taking care of that already
22+
*
23+
* @param $order_id - order ID
24+
* @return boolean true if WCS should send the tracking info, false otherwise
25+
*/
26+
public static function should_email_tracking_details( $order_id ) {
27+
if ( function_exists( 'wc_shipment_tracking' ) ) {
28+
$shipment_tracking = wc_shipment_tracking();
29+
if ( property_exists( $shipment_tracking, 'actions' )
30+
&& method_exists( $shipment_tracking->actions, 'get_tracking_items' ) ) {
31+
$shipment_tracking_items = $shipment_tracking->actions->get_tracking_items( $order_id );
32+
if ( ! empty( $shipment_tracking_items ) ) {
33+
return false;
34+
}
35+
}
36+
}
37+
38+
return true;
39+
}
40+
}
41+
}

classes/class-wc-connect-service-settings-store.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ public function update_label_order_meta_data( $order_id, $new_label_data ) {
248248
if ( $label_data['label_id'] === $new_label_data->label_id ) {
249249
$result = array_merge( $label_data, (array) $new_label_data );
250250
$labels_data[ $index ] = $result;
251+
252+
if ( ! isset( $label_data['tracking'] )
253+
&& isset( $result['tracking'] ) ) {
254+
WC_Connect_Extension_Compatibility::on_new_tracking_number( $order_id, $result['carrier_id'], $result['tracking'] );
255+
}
251256
}
252257
}
253258
update_post_meta( $order_id, 'wc_connect_labels', $labels_data );

woocommerce-services.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
require_once( plugin_basename( 'classes/class-wc-connect-options.php' ) );
3838
require_once( plugin_basename( 'classes/class-wc-connect-jetpack.php' ) );
39+
require_once( plugin_basename( 'classes/class-wc-connect-extension-compatibility.php' ) );
3940

4041
if ( ! class_exists( 'WC_Connect_Loader' ) ) {
4142

@@ -879,8 +880,10 @@ public function localize_and_enqueue_service_script( $method_id, $instance_id =
879880
public function add_tracking_info_to_emails( $order, $sent_to_admin, $plain_text ) {
880881
$id = WC_Connect_Compatibility::instance()->get_order_id( $order );
881882

882-
// Abort if no id was passed or if the order is not marked as 'completed'
883-
if ( ! $id || ! $order->has_status( 'completed' ) ) {
883+
// Abort if no id was passed, if the order is not marked as 'completed' or if another extension is handling the emailing
884+
if ( ! $id
885+
|| ! $order->has_status( 'completed' )
886+
|| ! WC_Connect_Extension_Compatibility::should_email_tracking_details( $id ) ) {
884887
return;
885888
}
886889

0 commit comments

Comments
 (0)