Merge pull request #2463 from woocommerce/PCP-3390-add-package-tracking-compatibility-with-dhl-shipping-plugin

Add Package Tracking compatibility with DHL Shipping plugin (3390)
This commit is contained in:
Emili Castells 2024-08-22 11:19:18 +02:00 committed by GitHub
commit 462d2d54dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 159 additions and 2 deletions

View file

@ -15,6 +15,8 @@ document.addEventListener( 'DOMContentLoaded', () => {
); );
const wcShipmentTaxBuyLabelButtonSelector = const wcShipmentTaxBuyLabelButtonSelector =
'.components-modal__screen-overlay .label-purchase-modal__sidebar .purchase-section button.components-button'; '.components-modal__screen-overlay .label-purchase-modal__sidebar .purchase-section button.components-button';
const dhlGenerateLabelButton =
document.getElementById( 'dhl-label-button' );
const toggleLoaderVisibility = function () { const toggleLoaderVisibility = function () {
const loader = document.querySelector( '.ppcp-tracking-loader' ); const loader = document.querySelector( '.ppcp-tracking-loader' );
@ -44,6 +46,20 @@ document.addEventListener( 'DOMContentLoaded', () => {
} }
}; };
const waitForButtonRemoval = function ( button ) {
if ( document.body.contains( button ) ) {
setTimeout( () => waitForButtonRemoval( button ), 100 );
} else {
jQuery( orderTrackingContainerSelector ).load(
loadLocation,
'',
function () {
toggleLoaderVisibility();
}
);
}
};
if ( if (
gzdSyncEnabled && gzdSyncEnabled &&
typeof gzdSaveButton !== 'undefined' && typeof gzdSaveButton !== 'undefined' &&
@ -66,10 +82,30 @@ document.addEventListener( 'DOMContentLoaded', () => {
} ); } );
} }
if (
typeof dhlGenerateLabelButton !== 'undefined' &&
dhlGenerateLabelButton != null
) {
dhlGenerateLabelButton.addEventListener( 'click', function ( event ) {
toggleLoaderVisibility();
waitForButtonRemoval( dhlGenerateLabelButton );
} );
}
jQuery( document ).on(
'mouseover mouseout',
'#dhl_delete_label',
function ( event ) {
jQuery( '#ppcp-shipment-status' )
.val( 'CANCELLED' )
.trigger( 'change' );
document.querySelector( '.update_shipment' ).click();
}
);
if ( if (
wcShippingTaxSyncEnabled && wcShippingTaxSyncEnabled &&
typeof wcShippingTaxSyncEnabled !== 'undefined' && typeof wcShippingTaxSyncEnabled !== 'undefined'
wcShippingTaxSyncEnabled != null
) { ) {
document.addEventListener( 'click', function ( event ) { document.addEventListener( 'click', function ( event ) {
const wcShipmentTaxBuyLabelButton = event.target.closest( const wcShipmentTaxBuyLabelButton = event.target.closest(

View file

@ -77,6 +77,9 @@ return array(
'compat.ywot.is_supported_plugin_version_active' => function (): bool { 'compat.ywot.is_supported_plugin_version_active' => function (): bool {
return function_exists( 'yith_ywot_init' ); return function_exists( 'yith_ywot_init' );
}, },
'compat.dhl.is_supported_plugin_version_active' => function (): bool {
return function_exists( 'PR_DHL' );
},
'compat.shipstation.is_supported_plugin_version_active' => function (): bool { 'compat.shipstation.is_supported_plugin_version_active' => function (): bool {
return function_exists( 'woocommerce_shipstation_init' ); return function_exists( 'woocommerce_shipstation_init' );
}, },

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\OrderTracking; namespace WooCommerce\PayPalCommerce\OrderTracking;
use WooCommerce\PayPalCommerce\OrderTracking\Integration\DhlShipmentIntegration;
use WooCommerce\PayPalCommerce\OrderTracking\Integration\GermanizedShipmentIntegration; use WooCommerce\PayPalCommerce\OrderTracking\Integration\GermanizedShipmentIntegration;
use WooCommerce\PayPalCommerce\OrderTracking\Integration\ShipmentTrackingIntegration; use WooCommerce\PayPalCommerce\OrderTracking\Integration\ShipmentTrackingIntegration;
use WooCommerce\PayPalCommerce\OrderTracking\Integration\ShipStationIntegration; use WooCommerce\PayPalCommerce\OrderTracking\Integration\ShipStationIntegration;
@ -118,6 +119,7 @@ return array(
$is_gzd_active = $container->get( 'compat.gzd.is_supported_plugin_version_active' ); $is_gzd_active = $container->get( 'compat.gzd.is_supported_plugin_version_active' );
$is_wc_shipment_active = $container->get( 'compat.wc_shipment_tracking.is_supported_plugin_version_active' ); $is_wc_shipment_active = $container->get( 'compat.wc_shipment_tracking.is_supported_plugin_version_active' );
$is_yith_ywot_active = $container->get( 'compat.ywot.is_supported_plugin_version_active' ); $is_yith_ywot_active = $container->get( 'compat.ywot.is_supported_plugin_version_active' );
$is_dhl_de_active = $container->get( 'compat.dhl.is_supported_plugin_version_active' );
$is_ship_station_active = $container->get( 'compat.shipstation.is_supported_plugin_version_active' ); $is_ship_station_active = $container->get( 'compat.shipstation.is_supported_plugin_version_active' );
$is_wc_shipping_tax_active = $container->get( 'compat.wc_shipping_tax.is_supported_plugin_version_active' ); $is_wc_shipping_tax_active = $container->get( 'compat.wc_shipping_tax.is_supported_plugin_version_active' );
@ -135,6 +137,10 @@ return array(
$integrations[] = new YithShipmentIntegration( $shipment_factory, $logger, $endpoint ); $integrations[] = new YithShipmentIntegration( $shipment_factory, $logger, $endpoint );
} }
if ( $is_dhl_de_active ) {
$integrations[] = new DhlShipmentIntegration( $shipment_factory, $logger, $endpoint );
}
if ( $is_ship_station_active ) { if ( $is_ship_station_active ) {
$integrations[] = new ShipStationIntegration( $shipment_factory, $logger, $endpoint ); $integrations[] = new ShipStationIntegration( $shipment_factory, $logger, $endpoint );
} }

View file

@ -0,0 +1,112 @@
<?php
/**
* The Shipment integration for DHL Shipping Germany for WooCommerce plugin.
*
* @package WooCommerce\PayPalCommerce\OrderTracking\Integration
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\OrderTracking\Integration;
use Psr\Log\LoggerInterface;
use WC_Order;
use Exception;
use WooCommerce\PayPalCommerce\Compat\Integration;
use WooCommerce\PayPalCommerce\OrderTracking\Endpoint\OrderTrackingEndpoint;
use WooCommerce\PayPalCommerce\OrderTracking\Shipment\ShipmentFactoryInterface;
use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait;
use function WooCommerce\PayPalCommerce\Api\ppcp_get_paypal_order;
/**
* Class DhlShipmentIntegration
*/
class DhlShipmentIntegration implements Integration {
use TransactionIdHandlingTrait;
/**
* The shipment factory.
*
* @var ShipmentFactoryInterface
*/
protected $shipment_factory;
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/**
* The order tracking endpoint.
*
* @var OrderTrackingEndpoint
*/
protected $endpoint;
/**
* The DhlShipmentIntegration constructor.
*
* @param ShipmentFactoryInterface $shipment_factory The shipment factory.
* @param LoggerInterface $logger The logger.
* @param OrderTrackingEndpoint $endpoint The order tracking endpoint.
*/
public function __construct(
ShipmentFactoryInterface $shipment_factory,
LoggerInterface $logger,
OrderTrackingEndpoint $endpoint
) {
$this->shipment_factory = $shipment_factory;
$this->logger = $logger;
$this->endpoint = $endpoint;
}
/**
* {@inheritDoc}
*/
public function integrate(): void {
add_action(
'pr_save_dhl_label_tracking',
function( int $order_id, array $tracking_details ) {
try {
$wc_order = wc_get_order( $order_id );
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return;
}
$paypal_order = ppcp_get_paypal_order( $wc_order );
$capture_id = $this->get_paypal_order_transaction_id( $paypal_order );
$tracking_number = $tracking_details['tracking_number'];
$carrier = $tracking_details['carrier'];
if ( ! $tracking_number || ! is_string( $tracking_number ) || ! $carrier || ! is_string( $carrier ) || ! $capture_id ) {
return;
}
$ppcp_shipment = $this->shipment_factory->create_shipment(
$order_id,
$capture_id,
$tracking_number,
'SHIPPED',
'DE_DHL',
$carrier,
array()
);
$tracking_information = $this->endpoint->get_tracking_information( $order_id, $tracking_number );
$tracking_information
? $this->endpoint->update_tracking_information( $ppcp_shipment, $order_id )
: $this->endpoint->add_tracking_information( $ppcp_shipment, $order_id );
} catch ( Exception $exception ) {
return;
}
},
600,
2
);
}
}