mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-04 08:47:23 +08:00
refactor tracking integration
This commit is contained in:
parent
1dffac6378
commit
c8e87120d4
24 changed files with 2915 additions and 964 deletions
|
@ -2,7 +2,7 @@
|
|||
"name": "ppcp-compat",
|
||||
"version": "1.0.0",
|
||||
"license": "GPL-3.0-or-later",
|
||||
"main": "resources/js/compat.js",
|
||||
"main": "resources/js/tracking-compat.js",
|
||||
"browserslist": [
|
||||
"> 0.5%",
|
||||
"Safari >= 8",
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
document.addEventListener(
|
||||
'DOMContentLoaded',
|
||||
() => {
|
||||
const orderTrackingContainerId = "ppcp_order-tracking";
|
||||
const orderTrackingContainerSelector = "#ppcp_order-tracking";
|
||||
const gzdSaveButton = document.getElementById('order-shipments-save');
|
||||
const loadLocation = location.href + " " + orderTrackingContainerSelector + ">*";
|
||||
|
||||
const setEnabled = function (enabled) {
|
||||
let childNodes = document.getElementById(orderTrackingContainerId).getElementsByTagName('*');
|
||||
for (let node of childNodes) {
|
||||
node.disabled = !enabled;
|
||||
}
|
||||
}
|
||||
|
||||
const waitForTrackingUpdate = function () {
|
||||
if (jQuery('#order-shipments-save').css('display') !== 'none') {
|
||||
setEnabled(false);
|
||||
setTimeout(waitForTrackingUpdate, 100)
|
||||
} else {
|
||||
jQuery(orderTrackingContainerSelector).load(loadLocation,"");
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof(gzdSaveButton) != 'undefined' && gzdSaveButton != null) {
|
||||
gzdSaveButton.addEventListener('click', function (event) {
|
||||
waitForTrackingUpdate();
|
||||
setEnabled(true);
|
||||
})
|
||||
}
|
||||
},
|
||||
);
|
49
modules/ppcp-compat/resources/js/tracking-compat.js
Normal file
49
modules/ppcp-compat/resources/js/tracking-compat.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
document.addEventListener(
|
||||
'DOMContentLoaded',
|
||||
() => {
|
||||
const config = PayPalCommerceGatewayOrderTrackingCompat;
|
||||
|
||||
const orderTrackingContainerId = "ppcp_order-tracking";
|
||||
const orderTrackingContainerSelector = "#ppcp_order-tracking .ppcp-tracking-column.shipments";
|
||||
const gzdSaveButton = document.getElementById('order-shipments-save');
|
||||
const loadLocation = location.href + " " + orderTrackingContainerSelector + ">*";
|
||||
const gzdSyncEnabled = config.gzd_sync_enabled;
|
||||
const wcShipmentSyncEnabled = config.wc_shipment_sync_enabled;
|
||||
const wcShipmentSaveButton = document.querySelector('#woocommerce-shipment-tracking .button-save-form');
|
||||
|
||||
const toggleLoaderVisibility = function() {
|
||||
const loader = document.querySelector('.ppcp-tracking-loader');
|
||||
if (loader) {
|
||||
if (loader.style.display === 'none' || loader.style.display === '') {
|
||||
loader.style.display = 'block';
|
||||
} else {
|
||||
loader.style.display = 'none';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const waitForTrackingUpdate = function (elementToCheck) {
|
||||
if (elementToCheck.css('display') !== 'none') {
|
||||
setTimeout(() => waitForTrackingUpdate(elementToCheck), 100);
|
||||
} else {
|
||||
jQuery(orderTrackingContainerSelector).load(loadLocation, "", function(){
|
||||
toggleLoaderVisibility();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (gzdSyncEnabled && typeof(gzdSaveButton) != 'undefined' && gzdSaveButton != null) {
|
||||
gzdSaveButton.addEventListener('click', function (event) {
|
||||
toggleLoaderVisibility();
|
||||
waitForTrackingUpdate(jQuery('#order-shipments-save'));
|
||||
})
|
||||
}
|
||||
|
||||
if (wcShipmentSyncEnabled && typeof(wcShipmentSaveButton) != 'undefined' && wcShipmentSaveButton != null) {
|
||||
wcShipmentSaveButton.addEventListener('click', function (event) {
|
||||
toggleLoaderVisibility();
|
||||
waitForTrackingUpdate(jQuery('#shipment-tracking-form'));
|
||||
})
|
||||
}
|
||||
},
|
||||
);
|
|
@ -11,7 +11,6 @@ namespace WooCommerce\PayPalCommerce\Compat;
|
|||
|
||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
return array(
|
||||
|
||||
|
@ -50,7 +49,7 @@ return array(
|
|||
'ppcp-webhooks-status-page',
|
||||
'ppcp-tracking',
|
||||
'ppcp-fraudnet',
|
||||
'ppcp-gzd-compat',
|
||||
'ppcp-tracking-compat',
|
||||
'ppcp-clear-db',
|
||||
);
|
||||
},
|
||||
|
@ -59,6 +58,10 @@ return array(
|
|||
return function_exists( 'wc_gzd_get_shipments_by_order' ); // 3.0+
|
||||
},
|
||||
|
||||
'compat.wc_shipment_tracking.is_supported_plugin_version_active' => function (): bool {
|
||||
return class_exists( 'WC_Shipment_Tracking' );
|
||||
},
|
||||
|
||||
'compat.module.url' => static function ( ContainerInterface $container ): string {
|
||||
/**
|
||||
* The path cannot be false.
|
||||
|
@ -75,18 +78,9 @@ return array(
|
|||
return new CompatAssets(
|
||||
$container->get( 'compat.module.url' ),
|
||||
$container->get( 'ppcp.asset-version' ),
|
||||
$container->get( 'order-tracking.is-paypal-order-edit-page' ) && $container->get( 'compat.should-initialize-gzd-compat-layer' )
|
||||
$container->get( 'order-tracking.is-module-enabled' ),
|
||||
$container->get( 'compat.gzd.is_supported_plugin_version_active' ),
|
||||
$container->get( 'compat.wc_shipment_tracking.is_supported_plugin_version_active' )
|
||||
);
|
||||
},
|
||||
|
||||
'compat.should-initialize-gzd-compat-layer' => function( ContainerInterface $container ) : bool {
|
||||
$settings = $container->get( 'wcgateway.settings' );
|
||||
assert( $settings instanceof Settings );
|
||||
|
||||
$tracking_enabled = $settings->has( 'tracking_enabled' ) && $settings->get( 'tracking_enabled' );
|
||||
$is_gzd_active = $container->get( 'compat.gzd.is_supported_plugin_version_active' );
|
||||
|
||||
return $tracking_enabled && $is_gzd_active;
|
||||
},
|
||||
|
||||
);
|
||||
|
|
|
@ -28,23 +28,48 @@ class CompatAssets {
|
|||
private $version;
|
||||
|
||||
/**
|
||||
* Whether Germanized synchronization scripts should be loaded.
|
||||
* Whether tracking compat scripts should be loaded.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $should_enqueue_gzd_scripts;
|
||||
protected $should_enqueue_tracking_scripts;
|
||||
|
||||
/**
|
||||
* Whether Germanized plugin is active.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $is_gzd_active;
|
||||
|
||||
/**
|
||||
* Whether WC Shipments plugin is active
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $is_wc_shipment_active;
|
||||
|
||||
/**
|
||||
* Compat module assets constructor.
|
||||
*
|
||||
* @param string $module_url The URL to the module.
|
||||
* @param string $version The assets version.
|
||||
* @param bool $should_enqueue_gzd_scripts Whether Germanized synchronization scripts should be loaded.
|
||||
* @param bool $should_enqueue_tracking_scripts Whether Germanized synchronization scripts should be loaded.
|
||||
* @param bool $is_gzd_active Whether Germanized plugin is active.
|
||||
* @param bool $is_wc_shipment_active Whether WC Shipments plugin is active.
|
||||
*/
|
||||
public function __construct( string $module_url, string $version, bool $should_enqueue_gzd_scripts ) {
|
||||
$this->module_url = $module_url;
|
||||
$this->version = $version;
|
||||
$this->should_enqueue_gzd_scripts = $should_enqueue_gzd_scripts;
|
||||
public function __construct(
|
||||
string $module_url,
|
||||
string $version,
|
||||
bool $should_enqueue_tracking_scripts,
|
||||
bool $is_gzd_active,
|
||||
bool $is_wc_shipment_active
|
||||
) {
|
||||
|
||||
$this->module_url = $module_url;
|
||||
$this->version = $version;
|
||||
$this->should_enqueue_tracking_scripts = $should_enqueue_tracking_scripts;
|
||||
$this->is_gzd_active = $is_gzd_active;
|
||||
$this->is_wc_shipment_active = $is_wc_shipment_active;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -53,15 +78,23 @@ class CompatAssets {
|
|||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
$gzd_sync_enabled = apply_filters( 'woocommerce_paypal_payments_sync_gzd_tracking', true );
|
||||
if ( $this->should_enqueue_gzd_scripts && $gzd_sync_enabled ) {
|
||||
if ( $this->should_enqueue_tracking_scripts ) {
|
||||
wp_register_script(
|
||||
'ppcp-gzd-compat',
|
||||
untrailingslashit( $this->module_url ) . '/assets/js/gzd-compat.js',
|
||||
'ppcp-tracking-compat',
|
||||
untrailingslashit( $this->module_url ) . '/assets/js/tracking-compat.js',
|
||||
array( 'jquery' ),
|
||||
$this->version,
|
||||
true
|
||||
);
|
||||
|
||||
wp_localize_script(
|
||||
'ppcp-tracking-compat',
|
||||
'PayPalCommerceGatewayOrderTrackingCompat',
|
||||
array(
|
||||
'gzd_sync_enabled' => apply_filters( 'woocommerce_paypal_payments_sync_gzd_tracking', true ) && $this->is_gzd_active,
|
||||
'wc_shipment_sync_enabled' => apply_filters( 'woocommerce_paypal_payments_sync_wc_shipment_tracking', true ) && $this->is_wc_shipment_active,
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -71,9 +104,8 @@ class CompatAssets {
|
|||
* @return void
|
||||
*/
|
||||
public function enqueue(): void {
|
||||
$gzd_sync_enabled = apply_filters( 'woocommerce_paypal_payments_sync_gzd_tracking', true );
|
||||
if ( $this->should_enqueue_gzd_scripts && $gzd_sync_enabled ) {
|
||||
wp_enqueue_script( 'ppcp-gzd-compat' );
|
||||
if ( $this->should_enqueue_tracking_scripts ) {
|
||||
wp_enqueue_script( 'ppcp-tracking-compat' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\Compat;
|
||||
|
||||
use WooCommerce\PayPalCommerce\OrderTracking\Shipment\ShipmentFactoryInterface;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
|
||||
use Exception;
|
||||
|
@ -21,7 +22,6 @@ use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets;
|
|||
use WooCommerce\PayPalCommerce\OrderTracking\Endpoint\OrderTrackingEndpoint;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
use WP_Theme;
|
||||
|
||||
/**
|
||||
* Class CompatModule
|
||||
|
@ -46,9 +46,14 @@ class CompatModule implements ModuleInterface {
|
|||
* @throws NotFoundException
|
||||
*/
|
||||
public function run( ContainerInterface $c ): void {
|
||||
|
||||
$this->initialize_ppec_compat_layer( $c );
|
||||
$this->fix_site_ground_optimizer_compatibility( $c );
|
||||
$this->initialize_gzd_compat_layer( $c );
|
||||
|
||||
$tracking_enabled = $c->get( 'order-tracking.is-module-enabled' );
|
||||
if ( $tracking_enabled ) {
|
||||
//$this->initialize_tracking_compat_layer( $c );
|
||||
}
|
||||
|
||||
$asset_loader = $c->get( 'compat.assets' );
|
||||
assert( $asset_loader instanceof CompatAssets );
|
||||
|
@ -114,6 +119,25 @@ class CompatModule implements ModuleInterface {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the 3rd party plugins compatibility layer for PayPal tracking.
|
||||
*
|
||||
* @param ContainerInterface $c The Container.
|
||||
* @return void
|
||||
*/
|
||||
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' );
|
||||
|
||||
if ( $is_gzd_active ) {
|
||||
$this->initialize_gzd_compat_layer( $c );
|
||||
}
|
||||
|
||||
if ( $is_wc_shipment_tracking_active ) {
|
||||
$this->initialize_wc_shipment_tracking_compat_layer( $c );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the <a href="https://wordpress.org/plugins/woocommerce-germanized/">Germanized for WooCommerce</a>
|
||||
* plugin compatibility layer.
|
||||
|
@ -124,19 +148,18 @@ class CompatModule implements ModuleInterface {
|
|||
* @return void
|
||||
*/
|
||||
protected function initialize_gzd_compat_layer( ContainerInterface $c ): void {
|
||||
if ( ! $c->get( 'compat.should-initialize-gzd-compat-layer' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$endpoint = $c->get( 'order-tracking.endpoint.controller' );
|
||||
assert( $endpoint instanceof OrderTrackingEndpoint );
|
||||
|
||||
$logger = $c->get( 'woocommerce.logger.woocommerce' );
|
||||
assert( $logger instanceof LoggerInterface );
|
||||
|
||||
$shipment_factory = $c->get( 'order-tracking.shipment.factory' );
|
||||
assert( $shipment_factory instanceof ShipmentFactoryInterface );
|
||||
|
||||
add_action(
|
||||
'woocommerce_gzd_shipment_status_shipped',
|
||||
static function( int $shipment_id, Shipment $shipment ) use ( $endpoint, $logger ) {
|
||||
static function( int $shipment_id, Shipment $shipment ) use ( $endpoint, $shipment_factory, $logger ) {
|
||||
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_gzd_tracking', true ) ) {
|
||||
return;
|
||||
}
|
||||
|
@ -146,35 +169,38 @@ class CompatModule implements ModuleInterface {
|
|||
return;
|
||||
}
|
||||
|
||||
$transaction_id = $wc_order->get_transaction_id();
|
||||
if ( empty( $transaction_id ) ) {
|
||||
$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 ( $item ) {
|
||||
return $item->get_order_item_id();
|
||||
},
|
||||
$shipment->get_items()
|
||||
);
|
||||
|
||||
if ( ! $tracking_number || ! $carrier || ! $transaction_id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tracking_data = array(
|
||||
'transaction_id' => $transaction_id,
|
||||
'status' => 'SHIPPED',
|
||||
);
|
||||
|
||||
$provider = $shipment->get_shipping_provider();
|
||||
if ( ! empty( $provider ) && $provider !== 'none' ) {
|
||||
/**
|
||||
* The filter allowing to change the default Germanized carrier for order tracking,
|
||||
* such as DHL_DEUTSCHE_POST, DPD_DE, ...
|
||||
*/
|
||||
$tracking_data['carrier'] = (string) apply_filters( 'woocommerce_paypal_payments_default_gzd_carrier', 'DHL_DEUTSCHE_POST', $provider );
|
||||
}
|
||||
|
||||
try {
|
||||
$tracking_information = $endpoint->get_tracking_information( $wc_order->get_id() );
|
||||
$ppcp_shipment = $shipment_factory->create_shipment(
|
||||
$order_id,
|
||||
$transaction_id,
|
||||
$tracking_number,
|
||||
'SHIPPED',
|
||||
'OTHER',
|
||||
$carrier,
|
||||
$items
|
||||
);
|
||||
|
||||
$tracking_data['tracking_number'] = $tracking_information['tracking_number'] ?? '';
|
||||
$tracking_information = $endpoint->get_tracking_information( $order_id, $tracking_number );
|
||||
|
||||
if ( $shipment->get_tracking_id() ) {
|
||||
$tracking_data['tracking_number'] = $shipment->get_tracking_id();
|
||||
}
|
||||
$tracking_information
|
||||
? $endpoint->update_tracking_information( $ppcp_shipment, $order_id )
|
||||
: $endpoint->add_tracking_information( $ppcp_shipment, $order_id );
|
||||
|
||||
! $tracking_information ? $endpoint->add_tracking_information( $tracking_data, $wc_order->get_id() ) : $endpoint->update_tracking_information( $tracking_data, $wc_order->get_id() );
|
||||
} catch ( Exception $exception ) {
|
||||
$logger->error( "Couldn't sync tracking information: " . $exception->getMessage() );
|
||||
}
|
||||
|
@ -184,6 +210,72 @@ class CompatModule implements ModuleInterface {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the <a href="https://woocommerce.com/document/shipment-tracking/">Shipment Tracking</a>
|
||||
* plugin compatibility layer.
|
||||
*
|
||||
* @link https://woocommerce.com/document/shipment-tracking/
|
||||
*
|
||||
* @param ContainerInterface $c The Container.
|
||||
* @return void
|
||||
*/
|
||||
protected function initialize_wc_shipment_tracking_compat_layer( ContainerInterface $c ): void {
|
||||
$endpoint = $c->get( 'order-tracking.endpoint.controller' );
|
||||
assert( $endpoint instanceof OrderTrackingEndpoint );
|
||||
|
||||
$logger = $c->get( 'woocommerce.logger.woocommerce' );
|
||||
assert( $logger instanceof LoggerInterface );
|
||||
|
||||
$shipment_factory = $c->get( 'order-tracking.shipment.factory' );
|
||||
assert( $shipment_factory instanceof ShipmentFactoryInterface );
|
||||
|
||||
add_action(
|
||||
'wp_ajax_wc_shipment_tracking_save_form',
|
||||
static function() use ( $endpoint, $shipment_factory, $logger ) {
|
||||
check_ajax_referer( 'create-tracking-item', 'security', true );
|
||||
|
||||
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_wc_shipment_tracking', true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$order_id = (int) wc_clean( wp_unslash( $_POST['order_id'] ?? 0 ) );
|
||||
$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['tracking_number'] ?? '' ) );
|
||||
$carrier = wc_clean( wp_unslash( $_POST['tracking_provider'] ?? '' ) );
|
||||
|
||||
if ( ! $tracking_number || ! $carrier || ! $transaction_id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$ppcp_shipment = $shipment_factory->create_shipment(
|
||||
$order_id,
|
||||
$transaction_id,
|
||||
$tracking_number,
|
||||
'SHIPPED',
|
||||
'OTHER',
|
||||
$carrier,
|
||||
array()
|
||||
);
|
||||
|
||||
$tracking_information = $endpoint->get_tracking_information( $order_id, $tracking_number );
|
||||
|
||||
$tracking_information
|
||||
? $endpoint->update_tracking_information( $ppcp_shipment, $order_id )
|
||||
: $endpoint->add_tracking_information( $ppcp_shipment, $order_id );
|
||||
|
||||
} catch ( Exception $exception ) {
|
||||
$logger->error( "Couldn't sync tracking information: " . $exception->getMessage() );
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrates the old Pay Later button and messaging settings for new Pay Later Tab.
|
||||
*
|
||||
|
|
|
@ -6,7 +6,7 @@ module.exports = {
|
|||
mode: isProduction ? 'production' : 'development',
|
||||
target: 'web',
|
||||
entry: {
|
||||
'gzd-compat': path.resolve('./resources/js/gzd-compat.js'),
|
||||
'tracking-compat': path.resolve('./resources/js/tracking-compat.js'),
|
||||
},
|
||||
output: {
|
||||
path: path.resolve(__dirname, 'assets/'),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue