From cb59f725a2f21c01ba893b0be8eec806b02af94f Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Fri, 1 Sep 2023 18:42:27 +0400 Subject: [PATCH] Add compat layer for Yith tracking --- modules/ppcp-compat/services.php | 18 ++++++---- modules/ppcp-compat/src/CompatModule.php | 42 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/modules/ppcp-compat/services.php b/modules/ppcp-compat/services.php index 3655be9fc..00b29f669 100644 --- a/modules/ppcp-compat/services.php +++ b/modules/ppcp-compat/services.php @@ -14,7 +14,7 @@ use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets; return array( - 'compat.ppec.mock-gateway' => static function( $container ) { + 'compat.ppec.mock-gateway' => static function( $container ) { $settings = $container->get( 'wcgateway.settings' ); $title = $settings->has( 'title' ) ? $settings->get( 'title' ) : __( 'PayPal', 'woocommerce-paypal-payments' ); $title = sprintf( @@ -26,20 +26,20 @@ return array( return new PPEC\MockGateway( $title ); }, - 'compat.ppec.subscriptions-handler' => static function ( ContainerInterface $container ) { + 'compat.ppec.subscriptions-handler' => static function ( ContainerInterface $container ) { $ppcp_renewal_handler = $container->get( 'subscription.renewal-handler' ); $gateway = $container->get( 'compat.ppec.mock-gateway' ); return new PPEC\SubscriptionsHandler( $ppcp_renewal_handler, $gateway ); }, - 'compat.ppec.settings_importer' => static function( ContainerInterface $container ) : PPEC\SettingsImporter { + 'compat.ppec.settings_importer' => static function( ContainerInterface $container ) : PPEC\SettingsImporter { $settings = $container->get( 'wcgateway.settings' ); return new PPEC\SettingsImporter( $settings ); }, - 'compat.plugin-script-names' => static function( ContainerInterface $container ) : array { + 'compat.plugin-script-names' => static function( ContainerInterface $container ) : array { return array( 'ppcp-smart-button', 'ppcp-oxxo', @@ -54,7 +54,7 @@ return array( ); }, - 'compat.gzd.is_supported_plugin_version_active' => function (): bool { + 'compat.gzd.is_supported_plugin_version_active' => function (): bool { return function_exists( 'wc_gzd_get_shipments_by_order' ); // 3.0+ }, @@ -62,7 +62,11 @@ return array( return class_exists( 'WC_Shipment_Tracking' ); }, - 'compat.module.url' => static function ( ContainerInterface $container ): string { + 'compat.ywot.is_supported_plugin_version_active' => function (): bool { + return function_exists( 'yith_ywot_init' ); + }, + + 'compat.module.url' => static function ( ContainerInterface $container ): string { /** * The path cannot be false. * @@ -74,7 +78,7 @@ return array( ); }, - 'compat.assets' => function( ContainerInterface $container ) : CompatAssets { + 'compat.assets' => function( ContainerInterface $container ) : CompatAssets { return new CompatAssets( $container->get( 'compat.module.url' ), $container->get( 'ppcp.asset-version' ), diff --git a/modules/ppcp-compat/src/CompatModule.php b/modules/ppcp-compat/src/CompatModule.php index 857411418..c2aa703d8 100644 --- a/modules/ppcp-compat/src/CompatModule.php +++ b/modules/ppcp-compat/src/CompatModule.php @@ -127,6 +127,7 @@ class CompatModule implements ModuleInterface { protected function initialize_tracking_compat_layer( ContainerInterface $c ): void { $is_gzd_active = $c->get( 'compat.gzd.is_supported_plugin_version_active' ); $is_wc_shipment_tracking_active = $c->get( 'compat.wc_shipment_tracking.is_supported_plugin_version_active' ); + $is_ywot_active = $c->get( 'compat.ywot.is_supported_plugin_version_active' ); if ( $is_gzd_active ) { $this->initialize_gzd_compat_layer( $c ); @@ -135,6 +136,10 @@ class CompatModule implements ModuleInterface { if ( $is_wc_shipment_tracking_active ) { $this->initialize_wc_shipment_tracking_compat_layer( $c ); } + + if ( $is_ywot_active ) { + $this->initialize_ywot_compat_layer( $c ); + } } /** @@ -258,6 +263,43 @@ class CompatModule implements ModuleInterface { ); } + /** + * Sets up the YITH WooCommerce Order & Shipment Tracking + * plugin compatibility layer. + * + * @link https://wordpress.org/plugins/yith-woocommerce-order-tracking/ + * + * @param ContainerInterface $c The Container. + * @return void + */ + protected function initialize_ywot_compat_layer( ContainerInterface $c ): void { + add_action( + 'woocommerce_process_shop_order_meta', + function( int $order_id ) use ( $c ) { + if ( ! apply_filters( 'woocommerce_paypal_payments_sync_ywot_tracking', true ) ) { + return; + } + + $wc_order = wc_get_order( $order_id ); + if ( ! is_a( $wc_order, WC_Order::class ) ) { + return; + } + + $transaction_id = $wc_order->get_transaction_id(); + $tracking_number = wc_clean( wp_unslash( $_POST['ywot_tracking_code'] ?? '' ) ); + $carrier = wc_clean( wp_unslash( $_POST['ywot_carrier_name'] ?? '' ) ); + + if ( ! $tracking_number || ! is_string( $tracking_number ) || ! $carrier || ! is_string( $carrier ) || ! $transaction_id ) { + return; + } + + $this->create_tracking( $c, $order_id, $transaction_id, $tracking_number, $carrier, array() ); + }, + 500, + 1 + ); + } + /** * Creates PayPal tracking. *