From cc0e4db9168daa056f04cbbed69d50f0bf6bded7 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Mon, 31 Jul 2023 18:08:00 +0400 Subject: [PATCH] Fix coding styles, psalm --- .psalm/gzd.php | 21 ++ modules/ppcp-compat/src/CompatModule.php | 16 +- modules/ppcp-order-tracking/carriers.php | 1 - .../resources/js/order-edit-page.js | 2 +- modules/ppcp-order-tracking/services.php | 10 +- .../src/Assets/OrderEditPageAssets.php | 2 +- .../src/Endpoint/OrderTrackingEndpoint.php | 267 ++++++++++-------- .../src/MetaBoxRenderer.php | 27 +- .../src/OrderTrackingModule.php | 42 +-- .../src/Shipment/Shipment.php | 14 +- .../src/Shipment/ShipmentInterface.php | 2 +- 11 files changed, 226 insertions(+), 178 deletions(-) diff --git a/.psalm/gzd.php b/.psalm/gzd.php index 6ececb60d..1b2b02256 100644 --- a/.psalm/gzd.php +++ b/.psalm/gzd.php @@ -50,6 +50,27 @@ namespace Vendidero\Germanized\Shipments { public function add_note( $note, $added_by_user = false ) { } + + /** + * Return an array of items within this shipment. + * + * @return ShipmentItem[] + */ + public function get_items() { + } + } + + class ShipmentItem extends WC_Data { + + /** + * Get order ID this meta belongs to. + * + * @param string $context What the value is for. Valid values are 'view' and 'edit'. + * @return int + */ + public function get_order_item_id( $context = 'view' ) { + } + } } diff --git a/modules/ppcp-compat/src/CompatModule.php b/modules/ppcp-compat/src/CompatModule.php index 7dfd7ad9c..bbebf1c36 100644 --- a/modules/ppcp-compat/src/CompatModule.php +++ b/modules/ppcp-compat/src/CompatModule.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Compat; +use Vendidero\Germanized\Shipments\ShipmentItem; use WooCommerce\PayPalCommerce\OrderTracking\Shipment\ShipmentFactoryInterface; use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider; use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface; @@ -49,11 +50,7 @@ class CompatModule implements ModuleInterface { $this->initialize_ppec_compat_layer( $c ); $this->fix_site_ground_optimizer_compatibility( $c ); - - $tracking_enabled = $c->get( 'order-tracking.is-module-enabled' ); - if ( $tracking_enabled ) { - //$this->initialize_tracking_compat_layer( $c ); - } + $this->initialize_tracking_compat_layer( $c ); $asset_loader = $c->get( 'compat.assets' ); assert( $asset_loader instanceof CompatAssets ); @@ -165,6 +162,7 @@ class CompatModule implements ModuleInterface { } $wc_order = $shipment->get_order(); + if ( ! is_a( $wc_order, WC_Order::class ) ) { return; } @@ -174,7 +172,7 @@ class CompatModule implements ModuleInterface { $tracking_number = $shipment->get_tracking_id(); $carrier = $shipment->get_shipping_provider(); $items = array_map( - function ( $item ) { + function ( ShipmentItem $item ): int { return $item->get_order_item_id(); }, $shipment->get_items() @@ -238,7 +236,7 @@ class CompatModule implements ModuleInterface { return; } - $order_id = (int) wc_clean( wp_unslash( $_POST['order_id'] ?? 0 ) ); + $order_id = (int) wc_clean( wp_unslash( $_POST['order_id'] ?? '' ) ); $wc_order = wc_get_order( $order_id ); if ( ! is_a( $wc_order, WC_Order::class ) ) { return; @@ -247,8 +245,10 @@ class CompatModule implements ModuleInterface { $transaction_id = $wc_order->get_transaction_id(); $tracking_number = wc_clean( wp_unslash( $_POST['tracking_number'] ?? '' ) ); $carrier = wc_clean( wp_unslash( $_POST['tracking_provider'] ?? '' ) ); + $carrier_other = wc_clean( wp_unslash( $_POST['custom_tracking_provider'] ?? '' ) ); + $carrier = $carrier ?: $carrier_other ?: ''; - if ( ! $tracking_number || ! $carrier || ! $transaction_id ) { + if ( ! $tracking_number || ! is_string( $tracking_number ) || ! $carrier || ! is_string( $carrier ) || ! $transaction_id ) { return; } diff --git a/modules/ppcp-order-tracking/carriers.php b/modules/ppcp-order-tracking/carriers.php index 73f74dab0..429f12a51 100644 --- a/modules/ppcp-order-tracking/carriers.php +++ b/modules/ppcp-order-tracking/carriers.php @@ -592,7 +592,6 @@ namespace WooCommerce\PayPalCommerce\OrderTracking; 'WHISTL_SFTP' => _x( 'Whistl', 'Name of carrier', 'woocommerce-paypal-payments' ), 'WINESHIPPING_WEBHOOK' => _x( 'Wineshipping', 'Name of carrier', 'woocommerce-paypal-payments' ), 'WISH_EMAIL_PUSH' => _x( 'Wish', 'Name of carrier', 'woocommerce-paypal-payments' ), - 'WOOYOUNG_LOGISTICS_SFTP' => _x( 'WOO YOUNG LOG', 'Name of carrier', 'woocommerce-paypal-payments' ), 'WOOYOUNG_LOGISTICS_SFTP' => _x( 'WOO YOUNG LOGISTICS CO., LTD.', 'Name of carrier', 'woocommerce-paypal-payments' ), 'WORLDCOURIER' => _x( 'World Courier', 'Name of carrier', 'woocommerce-paypal-payments' ), 'WORLDNET' => _x( 'Worldnet Logistics', 'Name of carrier', 'woocommerce-paypal-payments' ), diff --git a/modules/ppcp-order-tracking/resources/js/order-edit-page.js b/modules/ppcp-order-tracking/resources/js/order-edit-page.js index cae9b9985..d94af05a8 100644 --- a/modules/ppcp-order-tracking/resources/js/order-edit-page.js +++ b/modules/ppcp-order-tracking/resources/js/order-edit-page.js @@ -16,7 +16,7 @@ document.addEventListener( function toggleLineItemsSelectbox() { const selectContainer = document.getElementById('items-select-container'); - includeAllItemsCheckbox.addEventListener('change', function(){ + includeAllItemsCheckbox?.addEventListener('change', function(){ selectContainer.style.display = includeAllItemsCheckbox.checked ? 'none' : 'block'; }) } diff --git a/modules/ppcp-order-tracking/services.php b/modules/ppcp-order-tracking/services.php index 23f8b093a..722edcba4 100644 --- a/modules/ppcp-order-tracking/services.php +++ b/modules/ppcp-order-tracking/services.php @@ -35,7 +35,8 @@ return array( $container->get( 'woocommerce.logger.woocommerce' ), $container->get( 'button.request-data' ), $container->get( 'order-tracking.shipment.factory' ), - $container->get( 'order-tracking.allowed-shipping-statuses' ) + $container->get( 'order-tracking.allowed-shipping-statuses' ), + $container->get( 'order-tracking.is-merchant-country-us' ) ); }, 'order-tracking.module.url' => static function ( ContainerInterface $container ): string { @@ -53,7 +54,8 @@ return array( return new MetaBoxRenderer( $container->get( 'order-tracking.allowed-shipping-statuses' ), $container->get( 'order-tracking.available-carriers' ), - $container->get( 'order-tracking.endpoint.controller' ) + $container->get( 'order-tracking.endpoint.controller' ), + $container->get( 'order-tracking.is-merchant-country-us' ) ); }, 'order-tracking.allowed-shipping-statuses' => static function ( ContainerInterface $container ): array { @@ -113,4 +115,8 @@ return array( return $is_tracking_available && apply_filters( 'woocommerce_paypal_payments_shipment_tracking_enabled', true ); }, + + 'order-tracking.is-merchant-country-us' => static function ( ContainerInterface $container ): bool { + return $container->get( 'api.shop.country' ) === 'US'; + }, ); diff --git a/modules/ppcp-order-tracking/src/Assets/OrderEditPageAssets.php b/modules/ppcp-order-tracking/src/Assets/OrderEditPageAssets.php index 56742c56d..39706832b 100644 --- a/modules/ppcp-order-tracking/src/Assets/OrderEditPageAssets.php +++ b/modules/ppcp-order-tracking/src/Assets/OrderEditPageAssets.php @@ -84,7 +84,7 @@ class OrderEditPageAssets { 'tracking_info' => array( 'endpoint' => \WC_AJAX::get_endpoint( OrderTrackingEndpoint::ENDPOINT ), 'nonce' => wp_create_nonce( OrderTrackingEndpoint::nonce() ), - 'url' => admin_url( 'admin-ajax.php' ) + 'url' => admin_url( 'admin-ajax.php' ), ), ), ); diff --git a/modules/ppcp-order-tracking/src/Endpoint/OrderTrackingEndpoint.php b/modules/ppcp-order-tracking/src/Endpoint/OrderTrackingEndpoint.php index f55bf05b7..311eba5b2 100644 --- a/modules/ppcp-order-tracking/src/Endpoint/OrderTrackingEndpoint.php +++ b/modules/ppcp-order-tracking/src/Endpoint/OrderTrackingEndpoint.php @@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\OrderTracking\Endpoint; use Exception; use Psr\Log\LoggerInterface; +use stdClass; use WC_Order; use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait; @@ -32,9 +33,8 @@ use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait; * status: SupportedStatuses, * tracking_number: string, * carrier: string, - * items?: string, + * items?: list, * carrier_name_other?: string, - * order_id?: int * } * Class OrderTrackingEndpoint */ @@ -86,6 +86,13 @@ class OrderTrackingEndpoint { */ protected $allowed_statuses; + /** + * Whether new API should be used. + * + * @var bool + */ + protected $should_use_new_api; + /** * PartnersEndpoint constructor. * @@ -95,6 +102,7 @@ class OrderTrackingEndpoint { * @param RequestData $request_data The Request data. * @param ShipmentFactoryInterface $shipment_factory The ShipmentFactory. * @param string[] $allowed_statuses Allowed shipping statuses. + * @param bool $should_use_new_api Whether new API should be used. */ public function __construct( string $host, @@ -102,14 +110,16 @@ class OrderTrackingEndpoint { LoggerInterface $logger, RequestData $request_data, ShipmentFactoryInterface $shipment_factory, - array $allowed_statuses + array $allowed_statuses, + bool $should_use_new_api ) { - $this->host = $host; - $this->bearer = $bearer; - $this->logger = $logger; - $this->request_data = $request_data; - $this->shipment_factory = $shipment_factory; - $this->allowed_statuses = $allowed_statuses; + $this->host = $host; + $this->bearer = $bearer; + $this->logger = $logger; + $this->request_data = $request_data; + $this->shipment_factory = $shipment_factory; + $this->allowed_statuses = $allowed_statuses; + $this->should_use_new_api = $should_use_new_api; } /** @@ -160,64 +170,33 @@ class OrderTrackingEndpoint { * @throws RuntimeException If problem adding. */ public function add_tracking_information( ShipmentInterface $shipment, int $order_id ) : void { - $wc_order = wc_get_order( $order_id ); - $paypal_order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY ); - $host = trailingslashit( $this->host ); - $url = "{$host}v2/checkout/orders/{$paypal_order_id}/track"; - $shipment_data = $shipment->to_array( $order_id ); + $wc_order = wc_get_order( $order_id ); + if ( ! $wc_order instanceof WC_Order ) { + return; + } - unset( $shipment_data['transaction_id'] ); - $shipment_data['capture_id'] = $shipment->transaction_id(); + $shipment_request_data = $this->generate_request_data( $wc_order, $shipment ); - $args = array( - 'method' => 'POST', - 'headers' => $this->request_headers(), - 'body' => wp_json_encode( (array) apply_filters( 'woocommerce_paypal_payments_tracking_data_before_sending', $shipment_data, $order_id ) ), - ); + $url = $shipment_request_data['url'] ?? ''; + $args = $shipment_request_data['args'] ?? array(); - do_action( 'woocommerce_paypal_payments_before_tracking_is_added', $order_id, $shipment_data ); + if ( ! $url || empty( $args ) ) { + $this->throw_runtime_exception( $shipment_request_data, 'create' ); + } $response = $this->request( $url, $args ); if ( is_wp_error( $response ) ) { - $error = new RuntimeException( - 'Could not create order tracking information.' + $args = array( + 'args' => $args, + 'response' => $response, ); - $this->logger->log( - 'warning', - $error->getMessage(), - array( - 'args' => $args, - 'response' => $response, - ) - ); - throw $error; + $this->throw_runtime_exception( $args, 'create' ); } - /** - * Need to ignore Method WP_Error::offsetGet does not exist - * - * @psalm-suppress UndefinedMethod - */ - $json = json_decode( $response['body'] ); $status_code = (int) wp_remote_retrieve_response_code( $response ); - if ( 201 !== $status_code ) { - $error = new PayPalApiException( - $json, - $status_code - ); - $this->logger->log( - 'warning', - sprintf( - 'Failed to create order tracking information. PayPal API response: %1$s', - $error->getMessage() - ), - array( - 'args' => $args, - 'response' => $response, - ) - ); - throw $error; + if ( 201 !== $status_code && ! is_wp_error( $response ) ) { + $this->throw_paypal_api_exception( $status_code, $args, $response, 'create' ); } $this->save_tracking_metadata( $wc_order, $shipment->tracking_number(), array_keys( $shipment->line_items() ) ); @@ -237,7 +216,7 @@ class OrderTrackingEndpoint { $host = trailingslashit( $this->host ); $tracker_id = $this->find_tracker_id( $shipment->transaction_id(), $shipment->tracking_number() ); $url = "{$host}v1/shipping/trackers/{$tracker_id}"; - $shipment_data = $shipment->to_array( $order_id ); + $shipment_data = $shipment->to_array(); $args = array( 'method' => 'PUT', @@ -250,44 +229,16 @@ class OrderTrackingEndpoint { $response = $this->request( $url, $args ); if ( is_wp_error( $response ) ) { - $error = new RuntimeException( - 'Could not update order tracking information.' + $args = array( + 'args' => $args, + 'response' => $response, ); - $this->logger->log( - 'warning', - $error->getMessage(), - array( - 'args' => $args, - 'response' => $response, - ) - ); - throw $error; + $this->throw_runtime_exception( $args, 'update' ); } - /** - * Need to ignore Method WP_Error::offsetGet does not exist - * - * @psalm-suppress UndefinedMethod - */ - $json = json_decode( $response['body'] ); $status_code = (int) wp_remote_retrieve_response_code( $response ); - if ( 204 !== $status_code ) { - $error = new PayPalApiException( - $json, - $status_code - ); - $this->logger->log( - 'warning', - sprintf( - 'Failed to update the order tracking information. PayPal API response: %1$s', - $error->getMessage() - ), - array( - 'args' => $args, - 'response' => $response, - ) - ); - throw $error; + if ( 204 !== $status_code && ! is_wp_error( $response ) ) { + $this->throw_paypal_api_exception( $status_code, $args, $response, 'update' ); } do_action( 'woocommerce_paypal_payments_after_tracking_is_updated', $order_id, $response ); @@ -303,7 +254,11 @@ class OrderTrackingEndpoint { * @throws RuntimeException If problem getting. */ public function get_tracking_information( int $wc_order_id, string $tracking_number ) : ?ShipmentInterface { - $wc_order = wc_get_order( $wc_order_id ); + $wc_order = wc_get_order( $wc_order_id ); + if ( ! $wc_order instanceof WC_Order ) { + return null; + } + $host = trailingslashit( $this->host ); $tracker_id = $this->find_tracker_id( $wc_order->get_transaction_id(), $tracking_number ); $url = "{$host}v1/shipping/trackers/{$tracker_id}"; @@ -316,18 +271,11 @@ class OrderTrackingEndpoint { $response = $this->request( $url, $args ); if ( is_wp_error( $response ) ) { - $error = new RuntimeException( - 'Could not fetch the tracking information.' + $args = array( + 'args' => $args, + 'response' => $response, ); - $this->logger->log( - 'warning', - $error->getMessage(), - array( - 'args' => $args, - 'response' => $response, - ) - ); - throw $error; + $this->throw_runtime_exception( $args, 'fetch' ); } /** @@ -346,15 +294,18 @@ class OrderTrackingEndpoint { } /** - * Gets the tracking information of a given order. + * Gets the list of shipments of a given order. * * @param int $wc_order_id The order ID. - * @return array|null The tracking information. - * @psalm-return TrackingInfo|null + * @return ShipmentInterface[] The list of shipments. * @throws RuntimeException If problem getting. */ public function list_tracking_information( int $wc_order_id ) : ?array { - $wc_order = wc_get_order( $wc_order_id ); + $wc_order = wc_get_order( $wc_order_id ); + if ( ! $wc_order instanceof WC_Order ) { + return array(); + } + $host = trailingslashit( $this->host ); $transaction_id = $wc_order->get_transaction_id(); $url = "{$host}v1/shipping/trackers?transaction_id={$transaction_id}"; @@ -367,18 +318,11 @@ class OrderTrackingEndpoint { $response = $this->request( $url, $args ); if ( is_wp_error( $response ) ) { - $error = new RuntimeException( - 'Could not fetch the tracking information.' + $args = array( + 'args' => $args, + 'response' => $response, ); - $this->logger->log( - 'warning', - $error->getMessage(), - array( - 'args' => $args, - 'response' => $response, - ) - ); - throw $error; + $this->throw_runtime_exception( $args, 'fetch' ); } /** @@ -455,8 +399,7 @@ class OrderTrackingEndpoint { /** * Validates the requested tracking info. * - * @param array $tracking_info A map of tracking information keys to values. - * @psalm-param TrackingInfo $tracking_info + * @param array $tracking_info A map of tracking information keys to values. * @return void * @throws RuntimeException If validation failed. */ @@ -526,4 +469,88 @@ class OrderTrackingEndpoint { $wc_order->update_meta_data( OrderTrackingModule::PPCP_TRACKING_INFO_META_NAME, $tracking_meta ); $wc_order->save(); } + + /** + * Generates the request data. + * + * @param WC_Order $wc_order The WC order. + * @param ShipmentInterface $shipment The shipment. + * @return array + */ + protected function generate_request_data( WC_Order $wc_order, ShipmentInterface $shipment ): array { + $paypal_order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY ); + $host = trailingslashit( $this->host ); + $shipment_data = $shipment->to_array(); + + $old_api_data = $shipment_data; + unset( $old_api_data['items'] ); + $request_shipment_data = array( 'trackers' => array( $old_api_data ) ); + + if ( $this->should_use_new_api ) { + unset( $shipment_data['transaction_id'] ); + $shipment_data['capture_id'] = $shipment->transaction_id(); + $request_shipment_data = $shipment_data; + } + + $url = $this->should_use_new_api ? "{$host}v2/checkout/orders/{$paypal_order_id}/track" : "{$host}v1/shipping/trackers"; + $args = array( + 'method' => 'POST', + 'headers' => $this->request_headers(), + 'body' => wp_json_encode( (array) apply_filters( 'woocommerce_paypal_payments_tracking_data_before_sending', $request_shipment_data, $wc_order->get_id() ) ), + ); + + return array( + 'url' => $url, + 'args' => $args, + ); + } + + /** + * Throws PayPal APi exception and logs the error message with given arguments. + * + * @param int $status_code The response status code. + * @param array $args The arguments. + * @param array $response The request response. + * @param string $message_part The part of the message. + * @return void + * + * @throws PayPalApiException PayPal APi exception. + */ + protected function throw_paypal_api_exception( int $status_code, array $args, array $response, string $message_part ): void { + $error = new PayPalApiException( + json_decode( $response['body'] ), + $status_code + ); + $this->logger->log( + 'warning', + sprintf( + "Failed to {$message_part} order tracking information. PayPal API response: %s", + $error->getMessage() + ), + array( + 'args' => $args, + 'response' => $response, + ) + ); + throw $error; + } + + /** + * Throws the exception && logs the error message with given arguments. + * + * @param array $args The arguments. + * @param string $message_part The part of the message. + * @return void + * + * @throws RuntimeException The exception. + */ + protected function throw_runtime_exception( array $args, string $message_part ): void { + $error = new RuntimeException( "Could not {$message_part} the order tracking information." ); + $this->logger->log( + 'warning', + $error->getMessage(), + $args + ); + throw $error; + } } diff --git a/modules/ppcp-order-tracking/src/MetaBoxRenderer.php b/modules/ppcp-order-tracking/src/MetaBoxRenderer.php index 04b82d1b2..8aa9c08f2 100644 --- a/modules/ppcp-order-tracking/src/MetaBoxRenderer.php +++ b/modules/ppcp-order-tracking/src/MetaBoxRenderer.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\OrderTracking; +use WC_Order; use WooCommerce\PayPalCommerce\OrderTracking\Endpoint\OrderTrackingEndpoint; use WooCommerce\PayPalCommerce\OrderTracking\Shipment\ShipmentInterface; use WP_Post; @@ -46,6 +47,13 @@ class MetaBoxRenderer { */ protected $order_tracking_endpoint; + /** + * Whether new API should be used. + * + * @var bool + */ + protected $should_use_new_api; + /** * MetaBoxRenderer constructor. * @@ -53,16 +61,19 @@ class MetaBoxRenderer { * @param array $carriers Available shipping carriers. * @psalm-param Carriers $carriers * @param OrderTrackingEndpoint $order_tracking_endpoint The order tracking endpoint. + * @param bool $should_use_new_api Whether new API should be used. */ public function __construct( array $allowed_statuses, array $carriers, - OrderTrackingEndpoint $order_tracking_endpoint + OrderTrackingEndpoint $order_tracking_endpoint, + bool $should_use_new_api ) { $this->allowed_statuses = $allowed_statuses; $this->carriers = $carriers; $this->order_tracking_endpoint = $order_tracking_endpoint; + $this->should_use_new_api = $should_use_new_api; } /** @@ -71,8 +82,12 @@ class MetaBoxRenderer { * @param WP_Post $post The post object. */ public function render( WP_Post $post ): void { - $wc_order_id = $post->ID; - $wc_order = wc_get_order( $wc_order_id ); + $wc_order_id = $post->ID; + $wc_order = wc_get_order( $wc_order_id ); + if ( ! $wc_order instanceof WC_Order ) { + return; + } + $transaction_id = $wc_order->get_transaction_id() ?: ''; $order_items = $wc_order->get_items(); $order_item_count = ! empty( $order_items ) ? count( $order_items ) : 0; @@ -91,7 +106,7 @@ class MetaBoxRenderer {

- 1 ) : ?> + 1 && $this->should_use_new_api ) : ?>

@@ -99,7 +114,7 @@ class MetaBoxRenderer { @@ -140,7 +155,7 @@ class MetaBoxRenderer {

- +

diff --git a/modules/ppcp-order-tracking/src/OrderTrackingModule.php b/modules/ppcp-order-tracking/src/OrderTrackingModule.php index 5fb0d6e98..630b46eba 100644 --- a/modules/ppcp-order-tracking/src/OrderTrackingModule.php +++ b/modules/ppcp-order-tracking/src/OrderTrackingModule.php @@ -49,6 +49,11 @@ class OrderTrackingModule implements ModuleInterface { public function run( ContainerInterface $c ): void { $tracking_enabled = $c->get( 'order-tracking.is-module-enabled' ); + $endpoint = $c->get( 'order-tracking.endpoint.controller' ); + assert( $endpoint instanceof OrderTrackingEndpoint ); + + add_action( 'wc_ajax_' . OrderTrackingEndpoint::ENDPOINT, array( $endpoint, 'handle_request' ) ); + if ( ! $tracking_enabled ) { return; } @@ -56,15 +61,11 @@ class OrderTrackingModule implements ModuleInterface { $asset_loader = $c->get( 'order-tracking.assets' ); assert( $asset_loader instanceof OrderEditPageAssets ); - $endpoint = $c->get( 'order-tracking.endpoint.controller' ); - assert( $endpoint instanceof OrderTrackingEndpoint ); - $logger = $c->get( 'woocommerce.logger.woocommerce' ); assert( $logger instanceof LoggerInterface ); add_action( 'init', array( $asset_loader, 'register' ) ); add_action( 'admin_enqueue_scripts', array( $asset_loader, 'enqueue' ) ); - add_action( 'wc_ajax_' . OrderTrackingEndpoint::ENDPOINT, array( $endpoint, 'handle_request' ) ); $meta_box_renderer = $c->get( 'order-tracking.meta-box.renderer' ); add_action( @@ -75,38 +76,5 @@ class OrderTrackingModule implements ModuleInterface { 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 ( $tracking_information ) { - return; - } - - $wc_order = wc_get_order( $order_id ); - if ( ! is_a( $wc_order, WC_Order::class ) ) { - return; - } - - $transaction_id = $wc_order->get_transaction_id(); - if ( 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; - } - } - ); } } diff --git a/modules/ppcp-order-tracking/src/Shipment/Shipment.php b/modules/ppcp-order-tracking/src/Shipment/Shipment.php index 3b8fcd258..361989c72 100644 --- a/modules/ppcp-order-tracking/src/Shipment/Shipment.php +++ b/modules/ppcp-order-tracking/src/Shipment/Shipment.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\OrderTracking\Shipment; +use WC_Order; +use WC_Order_Item_Product; use WC_Product; use WooCommerce\PayPalCommerce\ApiClient\Entity\Item; use WooCommerce\PayPalCommerce\ApiClient\Entity\Money; @@ -135,7 +137,11 @@ class Shipment implements ShipmentInterface { * {@inheritDoc} */ public function line_items(): array { - $wc_order = wc_get_order( $this->wc_order_id ); + $wc_order = wc_get_order( $this->wc_order_id ); + if ( ! $wc_order instanceof WC_Order ) { + return array(); + } + $wc_order_items = $wc_order->get_items(); $tracking_meta = $wc_order->get_meta( OrderTrackingModule::PPCP_TRACKING_INFO_META_NAME ); $saved_line_items = $tracking_meta[ $this->tracking_number() ] ?? array(); @@ -143,6 +149,7 @@ class Shipment implements ShipmentInterface { $tracking_items = array(); foreach ( $wc_order_items as $item ) { + assert( $item instanceof WC_Order_Item_Product ); if ( ! empty( $line_items ) && ! in_array( $item->get_id(), $line_items, true ) ) { continue; } @@ -242,6 +249,11 @@ class Shipment implements ShipmentInterface { * @return void */ protected function render_shipment_line_item_info(): void { + $line_items = $this->line_items(); + if ( empty( $line_items ) ) { + return; + } + $format = '

%1$s %2$s

'; $order_items_info = array(); diff --git a/modules/ppcp-order-tracking/src/Shipment/ShipmentInterface.php b/modules/ppcp-order-tracking/src/Shipment/ShipmentInterface.php index a106fe034..29d7eecff 100644 --- a/modules/ppcp-order-tracking/src/Shipment/ShipmentInterface.php +++ b/modules/ppcp-order-tracking/src/Shipment/ShipmentInterface.php @@ -28,7 +28,7 @@ namespace WooCommerce\PayPalCommerce\OrderTracking\Shipment; * tracking_number: string, * status: string, * carrier: string, - * line_items: array, + * items: array, * carrier_name_other?: string, * } */