mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-30 05:00:51 +08:00
Add tracking info on order complete
This commit is contained in:
parent
3e47e62f78
commit
cd75b08e9e
1 changed files with 107 additions and 65 deletions
|
@ -11,8 +11,10 @@ namespace WooCommerce\PayPalCommerce\OrderTracking;
|
|||
|
||||
use Dhii\Container\ServiceProvider;
|
||||
use Dhii\Modular\Module\ModuleInterface;
|
||||
use Exception;
|
||||
use Interop\Container\ServiceProviderInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use WC_Order;
|
||||
use WooCommerce\PayPalCommerce\OrderTracking\Assets\OrderEditPageAssets;
|
||||
use WooCommerce\PayPalCommerce\OrderTracking\Endpoint\OrderTrackingEndpoint;
|
||||
|
@ -23,80 +25,120 @@ use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
|||
*/
|
||||
class OrderTrackingModule implements ModuleInterface {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function setup(): ServiceProviderInterface {
|
||||
return new ServiceProvider(
|
||||
require __DIR__ . '/../services.php',
|
||||
require __DIR__ . '/../extensions.php'
|
||||
);
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function setup(): ServiceProviderInterface {
|
||||
return new ServiceProvider(
|
||||
require __DIR__ . '/../services.php',
|
||||
require __DIR__ . '/../extensions.php'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @param ContainerInterface $c A services container instance.
|
||||
*/
|
||||
public function run( ContainerInterface $c ): void {
|
||||
$settings = $c->get( 'wcgateway.settings' );
|
||||
$trackingEnabled = $settings->has( 'tracking_enabled' ) && $settings->get( 'tracking_enabled' );
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @param ContainerInterface $c A services container instance.
|
||||
*/
|
||||
public function run( ContainerInterface $c ): void {
|
||||
$settings = $c->get( 'wcgateway.settings' );
|
||||
$tracking_enabled = $settings->has( 'tracking_enabled' ) && $settings->get( 'tracking_enabled' );
|
||||
|
||||
if ( !$trackingEnabled ) {
|
||||
return;
|
||||
}
|
||||
if ( ! $tracking_enabled ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$asset_loader = $c->get('order-tracking.assets');
|
||||
assert( $asset_loader instanceof OrderEditPageAssets );
|
||||
$isPayPalOrderEditPage = $c->get( 'order-tracking.is-paypal-order-edit-page' );
|
||||
$asset_loader = $c->get( 'order-tracking.assets' );
|
||||
assert( $asset_loader instanceof OrderEditPageAssets );
|
||||
$is_paypal_order_edit_page = $c->get( 'order-tracking.is-paypal-order-edit-page' );
|
||||
|
||||
add_action(
|
||||
'init',
|
||||
function () use ($asset_loader, $isPayPalOrderEditPage) {
|
||||
if(!$isPayPalOrderEditPage) {
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* The tracking Endpoint.
|
||||
*
|
||||
* @var OrderTrackingEndpoint $endpoint
|
||||
*/
|
||||
$endpoint = $c->get( 'order-tracking.endpoint.controller' );
|
||||
|
||||
$asset_loader->register();
|
||||
}
|
||||
);
|
||||
/**
|
||||
* The logger.
|
||||
*
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
$logger = $c->get( 'woocommerce.logger.woocommerce' );
|
||||
|
||||
add_action(
|
||||
'admin_enqueue_scripts',
|
||||
function () use ($asset_loader, $isPayPalOrderEditPage) {
|
||||
if(!$isPayPalOrderEditPage) {
|
||||
return;
|
||||
}
|
||||
add_action(
|
||||
'init',
|
||||
static function () use ( $asset_loader, $is_paypal_order_edit_page ) {
|
||||
if ( ! $is_paypal_order_edit_page ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$asset_loader->enqueue();
|
||||
}
|
||||
);
|
||||
$asset_loader->register();
|
||||
}
|
||||
);
|
||||
|
||||
add_action(
|
||||
'wc_ajax_' . OrderTrackingEndpoint::ENDPOINT,
|
||||
static function () use ( $c ) {
|
||||
$endpoint = $c->get( 'order-tracking.endpoint.controller' );
|
||||
/**
|
||||
* The tracking Endpoint.
|
||||
*
|
||||
* @var OrderTrackingEndpoint $endpoint
|
||||
*/
|
||||
$endpoint->handle_request();
|
||||
}
|
||||
);
|
||||
add_action(
|
||||
'admin_enqueue_scripts',
|
||||
static function () use ( $asset_loader, $is_paypal_order_edit_page ) {
|
||||
if ( ! $is_paypal_order_edit_page ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$meta_box_renderer = $c->get('order-tracking.meta-box.renderer');
|
||||
add_action( 'add_meta_boxes', function() use ($meta_box_renderer, $isPayPalOrderEditPage) {
|
||||
if(!$isPayPalOrderEditPage) {
|
||||
return;
|
||||
}
|
||||
$asset_loader->enqueue();
|
||||
}
|
||||
);
|
||||
|
||||
add_meta_box( 'ppcp_order-tracking', __('Tracking Information','woocommerce-paypal-payments'), [$meta_box_renderer, 'render'], 'shop_order', 'side');
|
||||
},10, 2 );
|
||||
}
|
||||
add_action(
|
||||
'wc_ajax_' . OrderTrackingEndpoint::ENDPOINT,
|
||||
array( $endpoint, 'handle_request' )
|
||||
);
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getKey() { }
|
||||
$meta_box_renderer = $c->get( 'order-tracking.meta-box.renderer' );
|
||||
add_action(
|
||||
'add_meta_boxes',
|
||||
static function() use ( $meta_box_renderer, $is_paypal_order_edit_page ) {
|
||||
if ( ! $is_paypal_order_edit_page ) {
|
||||
return;
|
||||
}
|
||||
|
||||
add_meta_box( 'ppcp_order-tracking', __( 'Tracking Information', 'woocommerce-paypal-payments' ), array( $meta_box_renderer, 'render' ), 'shop_order', 'side' );
|
||||
},
|
||||
10,
|
||||
2
|
||||
);
|
||||
|
||||
add_action(
|
||||
'woocommerce_order_status_completed',
|
||||
static function( int $order_id ) use ( $endpoint, $logger ) {
|
||||
$tracking_information = $endpoint->get_tracking_information( $order_id );
|
||||
|
||||
if ( ! empty( $tracking_information ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$wc_order = wc_get_order( $order_id );
|
||||
$transaction_id = $wc_order->get_transaction_id();
|
||||
if ( ! is_a( $wc_order, WC_Order::class ) || empty( $transaction_id ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tracking_data = array(
|
||||
'transaction_id' => $transaction_id,
|
||||
'status' => 'SHIPPED',
|
||||
);
|
||||
|
||||
try {
|
||||
$endpoint->add_tracking_information( $tracking_data, $order_id );
|
||||
} catch ( Exception $exception ) {
|
||||
$logger->error( "Couldn't create tracking information: " . $exception->getMessage() );
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getKey() { }
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue