Sync GZD shipment only if status is changed to "shipped"

This commit is contained in:
Narek Zakarian 2022-11-30 17:22:25 +04:00
parent a0472e3a6a
commit 8e7ca29117
No known key found for this signature in database
GPG key ID: 07AFD7E7A9C164A7
2 changed files with 6 additions and 20 deletions

View file

@ -56,15 +56,6 @@ return array(
return function_exists( 'wc_gzd_get_shipments_by_order' ); // 3.0+ return function_exists( 'wc_gzd_get_shipments_by_order' ); // 3.0+
}, },
'compat.gzd.tracking_statuses_map' => function ( ContainerInterface $container ): array {
return array(
'draft' => 'ON_HOLD',
'processing' => 'SHIPPED',
'shipped' => 'SHIPPED',
'delivered' => 'DELIVERED',
);
},
'compat.module.url' => static function ( ContainerInterface $container ): string { 'compat.module.url' => static function ( ContainerInterface $container ): string {
/** /**
* The path cannot be false. * The path cannot be false.

View file

@ -130,20 +130,13 @@ class CompatModule implements ModuleInterface {
$logger = $c->get( 'woocommerce.logger.woocommerce' ); $logger = $c->get( 'woocommerce.logger.woocommerce' );
assert( $logger instanceof LoggerInterface ); assert( $logger instanceof LoggerInterface );
$status_map = $c->get( 'compat.gzd.tracking_statuses_map' );
add_action( add_action(
'woocommerce_gzd_shipment_after_save', 'woocommerce_gzd_shipment_status_shipped',
static function( Shipment $shipment ) use ( $endpoint, $logger, $status_map ) { static function( int $shipment_id, Shipment $shipment ) use ( $endpoint, $logger ) {
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_gzd_tracking', true ) ) { if ( ! apply_filters( 'woocommerce_paypal_payments_sync_gzd_tracking', true ) ) {
return; return;
} }
$gzd_shipment_status = $shipment->get_status();
if ( ! array_key_exists( $gzd_shipment_status, $status_map ) ) {
return;
}
$wc_order = $shipment->get_order(); $wc_order = $shipment->get_order();
if ( ! is_a( $wc_order, WC_Order::class ) ) { if ( ! is_a( $wc_order, WC_Order::class ) ) {
return; return;
@ -156,7 +149,7 @@ class CompatModule implements ModuleInterface {
$tracking_data = array( $tracking_data = array(
'transaction_id' => $transaction_id, 'transaction_id' => $transaction_id,
'status' => (string) $status_map[ $gzd_shipment_status ], 'status' => 'SHIPPED',
); );
$provider = $shipment->get_shipping_provider(); $provider = $shipment->get_shipping_provider();
@ -177,7 +170,9 @@ class CompatModule implements ModuleInterface {
} catch ( Exception $exception ) { } catch ( Exception $exception ) {
$logger->error( "Couldn't sync tracking information: " . $exception->getMessage() ); $logger->error( "Couldn't sync tracking information: " . $exception->getMessage() );
} }
} },
500,
2
); );
} }