diff --git a/modules/ppcp-compat/src/Integration.php b/modules/ppcp-compat/src/Integration.php index 62055c915..f8aaf8bc6 100644 --- a/modules/ppcp-compat/src/Integration.php +++ b/modules/ppcp-compat/src/Integration.php @@ -9,10 +9,10 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Compat; -interface Integration -{ - /** - * Integrates some (possibly external) service with PayPal Payments. - */ - public function integrate(): void; +interface Integration { + + /** + * Integrates some (possibly external) service with PayPal Payments. + */ + public function integrate(): void; } diff --git a/modules/ppcp-order-tracking/src/Integration/GermanizedShipmentIntegration.php b/modules/ppcp-order-tracking/src/Integration/GermanizedShipmentIntegration.php new file mode 100644 index 000000000..7e44fd765 --- /dev/null +++ b/modules/ppcp-order-tracking/src/Integration/GermanizedShipmentIntegration.php @@ -0,0 +1,123 @@ +shipment_factory = $shipment_factory; + $this->logger = $logger; + $this->endpoint = $endpoint; + } + + /** + * {@inheritDoc} + */ + public function integrate(): void { + + add_action( + 'woocommerce_gzd_shipment_status_shipped', + function( int $shipment_id, Shipment $shipment ) { + if ( ! apply_filters( 'woocommerce_paypal_payments_sync_gzd_tracking', true ) ) { + return; + } + + $wc_order = $shipment->get_order(); + + if ( ! is_a( $wc_order, WC_Order::class ) ) { + return; + } + + $wc_order_id = $wc_order->get_id(); + $transaction_id = $wc_order->get_transaction_id(); + $tracking_number = $shipment->get_tracking_id(); + $carrier = $shipment->get_shipping_provider(); + + $items = array_map( + function ( ShipmentItem $item ): int { + return $item->get_order_item_id(); + }, + $shipment->get_items() + ); + + if ( ! $tracking_number || ! $carrier || ! $transaction_id ) { + return; + } + + try { + $ppcp_shipment = $this->shipment_factory->create_shipment( + $wc_order_id, + $transaction_id, + $tracking_number, + 'SHIPPED', + 'OTHER', + $carrier, + $items + ); + + $tracking_information = $this->endpoint->get_tracking_information( $wc_order_id, $tracking_number ); + + $tracking_information + ? $this->endpoint->update_tracking_information( $ppcp_shipment, $wc_order_id ) + : $this->endpoint->add_tracking_information( $ppcp_shipment, $wc_order_id ); + + } catch ( Exception $exception ) { + $this->logger->error( "Couldn't sync tracking information: " . $exception->getMessage() ); + } + }, + 500, + 2 + ); + } +}