| @@ -452,14 +452,60 @@ | ||
| 452 | 452 | */ |
| 453 | 453 | public function create_shipment($tracking_id, $data = array()) |
| 454 | 454 | { |
| 455 | 455 | if (empty($tracking_id)) return false; |
| 456 | - return $this->_make_private_api_call('shipments', array( | |
| 456 | + $payload = array( | |
| 457 | 457 | 'tracking_number' => $this->sanitize_value($tracking_id), |
| 458 | - 'source_order_id' => $data['source_order_id'], | |
| 459 | - 'source_product_ids' => $data['source_product_ids'], | |
| 460 | - 'courier_id' => $this->sanitize_value($data['courier_id']), | |
| 461 | - ), 'POST'); | |
| 458 | + 'source_order_id' => isset($data['source_order_id']) ? $data['source_order_id'] : null, | |
| 459 | + 'source_product_ids' => isset($data['source_product_ids']) ? $data['source_product_ids'] : array(), | |
| 460 | + 'courier_id' => $this->sanitize_value(isset($data['courier_id']) ? $data['courier_id'] : ''), | |
| 461 | + ); | |
| 462 | + $response = $this->_make_private_api_call('shipments', $payload, 'POST'); | |
| 463 | + | |
| 464 | + return $this->retry_create_shipment_with_parent_product_ids($payload, $response); | |
| 465 | + } | |
| 466 | + | |
| 467 | + /** | |
| 468 | + * If the first create-shipment is rejected (HTTP 400, typically Invalid | |
| 469 | + * ProductIDs), retry once with parent product_ids from the Woo order. | |
| 470 | + * Those match webhook order-create when variation meta never reaches Route. | |
| 471 | + * | |
| 472 | + * @param array $payload Original shipment payload. | |
| 473 | + * @param array|\WP_Error $response First create-shipment response. | |
| 474 | + * @return array|\WP_Error | |
| 475 | + */ | |
| 476 | + private function retry_create_shipment_with_parent_product_ids($payload, $response) | |
| 477 | + { | |
| 478 | + if ( ! class_exists('Routeapp_WooCommerce_Common_Tracking_Provider') ) { | |
| 479 | + return $response; | |
| 480 | + } | |
| 481 | + | |
| 482 | + if ( Routeapp_WooCommerce_Common_Tracking_Provider::is_successful_shipment_create_response($response) ) { | |
| 483 | + return $response; | |
| 484 | + } | |
| 485 | + | |
| 486 | + $code = 0; | |
| 487 | + if ( isset($response['response']['code']) ) { | |
| 488 | + $code = (int) $response['response']['code']; | |
| 489 | + } elseif ( function_exists('wp_remote_retrieve_response_code') ) { | |
| 490 | + $code = (int) wp_remote_retrieve_response_code($response); | |
| 491 | + } | |
| 492 | + if ( $code !== 400 || empty($payload['source_order_id']) ) { | |
| 493 | + return $response; | |
| 494 | + } | |
| 495 | + | |
| 496 | + $parent_ids = Routeapp_WooCommerce_Common_Tracking_Provider::get_order_products_parent_ids($payload['source_order_id']); | |
| 497 | + if ( empty($parent_ids) ) { | |
| 498 | + return $response; | |
| 499 | + } | |
| 500 | + | |
| 501 | + $original_ids = array_map('strval', (array) $payload['source_product_ids']); | |
| 502 | + if ( $original_ids === $parent_ids ) { | |
| 503 | + return $response; | |
| 504 | + } | |
| 505 | + | |
| 506 | + $payload['source_product_ids'] = $parent_ids; | |
| 507 | + return $this->_make_private_api_call('shipments', $payload, 'POST'); | |
| 462 | 508 | } |
| 463 | 509 | |
| 464 | 510 | /** |
| 465 | 511 | * |