Add compat layer for Yith tracking

This commit is contained in:
Narek Zakarian 2023-09-01 18:42:27 +04:00
parent 1942acf67b
commit cb59f725a2
No known key found for this signature in database
GPG key ID: 07AFD7E7A9C164A7
2 changed files with 53 additions and 7 deletions

View file

@ -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' ),

View file

@ -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 <a href="https://wordpress.org/plugins/yith-woocommerce-order-tracking/">YITH WooCommerce Order & Shipment Tracking</a>
* 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.
*