Skip to content

Commit ce9b1a0

Browse files
authored
Merge pull request #1325 from Automattic/add/1156-wc-st-integration
Added integration with WooCommerce Shipment Tracking
2 parents ed893cc + 931ccb7 commit ce9b1a0

File tree

6 files changed

+89
-3
lines changed

6 files changed

+89
-3
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 );

client/apps/shipping-label/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import notices from 'state/notices/reducer';
1212
import reducer from 'woocommerce/woocommerce-services/state/shipping-label/reducer';
1313
import packagesReducer from 'woocommerce/woocommerce-services/state/packages/reducer';
1414
import labelSettingsReducer from 'woocommerce/woocommerce-services/state/label-settings/reducer';
15+
import reduxMiddleware from './redux-middleware';
1516
import { combineReducers } from 'state/utils';
1617

1718
export default ( { orderId } ) => ( {
@@ -57,6 +58,10 @@ export default ( { orderId } ) => ( {
5758
return `wcs-label-${ orderId }`;
5859
},
5960

61+
getMiddleware() {
62+
return reduxMiddleware;
63+
},
64+
6065
View: () => (
6166
<ShippingLabelViewWrapper orderId={ orderId } />
6267
),
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
//from calypso
5+
import {
6+
WOOCOMMERCE_SERVICES_SHIPPING_LABEL_PURCHASE_RESPONSE,
7+
} from 'woocommerce/woocommerce-services/state/action-types';
8+
9+
const middlewareActions = {
10+
[ WOOCOMMERCE_SERVICES_SHIPPING_LABEL_PURCHASE_RESPONSE ]: ( { error } ) => {
11+
if ( error ) {
12+
return;
13+
}
14+
window.wc_shipment_tracking_refresh && window.wc_shipment_tracking_refresh();
15+
},
16+
};
17+
18+
export default () => ( next ) => ( action ) => {
19+
// let the action go to the reducers
20+
next( action );
21+
22+
const middlewareAction = middlewareActions[ action.type ];
23+
if ( ! middlewareAction ) {
24+
return;
25+
}
26+
27+
// perform the action
28+
middlewareAction( action );
29+
};

client/main.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ Array.from( document.getElementsByClassName( 'wcc-root' ) ).forEach( ( container
6666
Route.getReducer(),
6767
{ ...serverState, ...persistedState },
6868
compose(
69-
applyMiddleware( thunk.withExtraArgument( args ) ),
69+
applyMiddleware(
70+
thunk.withExtraArgument( args ),
71+
Route.getMiddleware ? Route.getMiddleware() : () => ( next ) => ( action ) => next( action ),
72+
),
7073
window.devToolsExtension ? window.devToolsExtension() : f => f
7174
)
7275
);

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)