| @@ -8,9 +8,10 @@ | ||
| 8 | 8 | declare( strict_types=1 ); |
| 9 | 9 | |
| 10 | 10 | namespace Packetery\Module\Order; |
| 11 | 11 | |
| 12 | -use Packetery\Module\Exception\InvalidCarrierException; | |
| 12 | +use Packetery\Core\Entity\Order; | |
| 13 | +use Packetery\Module\Shipping\ShippingProvider; | |
| 13 | 14 | use WC_Data; |
| 14 | 15 | use WC_Order; |
| 15 | 16 | use WP_REST_Response; |
| 16 | 17 | |
| @@ -47,53 +48,58 @@ | ||
| 47 | 48 | /** |
| 48 | 49 | * Extends WooCommerce API response with plugin data. |
| 49 | 50 | * |
| 50 | 51 | * @param WP_REST_Response $response Response. |
| 51 | - * @param WC_Data $object Object. | |
| 52 | + * @param WC_Data $wcData Object. | |
| 52 | 53 | * |
| 53 | 54 | * @return object |
| 54 | 55 | */ |
| 55 | - public function extendResponse( WP_REST_Response $response, WC_Data $object ): object { | |
| 56 | - if ( ! $object instanceof WC_Order ) { | |
| 56 | + public function extendResponse( WP_REST_Response $response, WC_Data $wcData ): object { | |
| 57 | + if ( ! $wcData instanceof WC_Order ) { | |
| 57 | 58 | return $response; |
| 58 | 59 | } |
| 59 | 60 | |
| 60 | - try { | |
| 61 | - $order = $this->orderRepository->getByWcOrder( $object ); | |
| 62 | - } catch ( InvalidCarrierException $invalidCarrierException ) { | |
| 61 | + $responseData = $response->get_data(); | |
| 62 | + if ( ! isset( $responseData['shipping_lines'] ) ) { | |
| 63 | 63 | return $response; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - if ( null === $order ) { | |
| 66 | + $order = $this->orderRepository->getByWcOrderWithValidCarrier( $wcData ); | |
| 67 | + if ( $order === null ) { | |
| 67 | 68 | return $response; |
| 68 | 69 | } |
| 69 | 70 | |
| 70 | - $this->addMetaDataItem( $response, '_packetery_carrier_id', $order->getCarrier()->getId() ); | |
| 71 | - if ( null !== $order->getPickupPoint() && null !== $order->getPickupPoint()->getId() ) { | |
| 72 | - $this->addMetaDataItem( $response, '_packetery_point_id', $order->getPickupPoint()->getId() ); | |
| 71 | + foreach ( $responseData['shipping_lines'] as $key => $shippingLine ) { | |
| 72 | + if ( ! ShippingProvider::isPacketaMethod( $shippingLine['method_id'] ) ) { | |
| 73 | + continue; | |
| 74 | + } | |
| 75 | + $response->data['shipping_lines'][ $key ]['packeta'] = $this->getPacketaItemsToShippingLines( $order ); | |
| 73 | 76 | } |
| 74 | - if ( null !== $order->getCarrierNumber() ) { | |
| 75 | - $this->addMetaDataItem( $response, '_packetery_carrier_number', $order->getCarrierNumber() ); | |
| 76 | - } | |
| 77 | - if ( null !== $order->getPacketId() ) { | |
| 78 | - $this->addMetaDataItem( $response, '_packetery_packet_id', $order->getPacketId() ); | |
| 79 | - } | |
| 80 | 77 | |
| 81 | 78 | return $response; |
| 82 | 79 | } |
| 83 | 80 | |
| 84 | 81 | /** |
| 85 | - * Adds metadata item to response. | |
| 82 | + * Add Items to Shipping Lines. | |
| 86 | 83 | * |
| 87 | - * @param WP_REST_Response $response Response. | |
| 88 | - * @param string $key Key. | |
| 89 | - * @param string $value Value. | |
| 84 | + * @param Order $order Packetery Order. | |
| 90 | 85 | * |
| 91 | - * @return void | |
| 86 | + * @return array<string, string|null> | |
| 92 | 87 | */ |
| 93 | - private function addMetaDataItem( WP_REST_Response $response, string $key, string $value ): void { | |
| 94 | - $response->data['meta_data'][] = [ | |
| 95 | - 'key' => $key, | |
| 96 | - 'value' => $value, | |
| 88 | + private function getPacketaItemsToShippingLines( Order $order ): array { | |
| 89 | + $items = [ | |
| 90 | + 'carrier_id' => $order->getCarrier()->getId(), | |
| 91 | + 'point_id' => null, | |
| 92 | + 'point_name' => null, | |
| 97 | 93 | ]; |
| 94 | + | |
| 95 | + if ( $order->getPickupPoint() !== null ) { | |
| 96 | + $items['point_id'] = $order->getPickupPoint()->getId(); | |
| 97 | + $items['point_name'] = $order->getPickupPoint()->getName(); | |
| 98 | + } | |
| 99 | + | |
| 100 | + $items['carrier_number'] = $order->getCarrierNumber(); | |
| 101 | + $items['packet_id'] = $order->getPacketId(); | |
| 102 | + | |
| 103 | + return $items; | |
| 98 | 104 | } |
| 99 | 105 | } |