| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class ApiExtender. |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Order; |
| 11 |
|
| 12 |
use Packetery\Core\Entity\Order; |
| 13 |
use Packetery\Module\Shipping\ShippingProvider; |
| 14 |
use WC_Data; |
| 15 |
use WC_Order; |
| 16 |
use WP_REST_Response; |
| 17 |
|
| 18 |
/** |
| 19 |
* Class ApiExtender. |
| 20 |
*/ |
| 21 |
class ApiExtender { |
| 22 |
|
| 23 |
/** |
| 24 |
* Order repository. |
| 25 |
* |
| 26 |
* @var Repository |
| 27 |
*/ |
| 28 |
private $orderRepository; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor. |
| 32 |
* |
| 33 |
* @param Repository $orderRepository Order repository. |
| 34 |
*/ |
| 35 |
public function __construct( Repository $orderRepository ) { |
| 36 |
$this->orderRepository = $orderRepository; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Registers service. |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function register(): void { |
| 45 |
add_filter( 'woocommerce_rest_prepare_shop_order_object', [ $this, 'extendResponse' ], 20, 2 ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Extends WooCommerce API response with plugin data. |
| 50 |
* |
| 51 |
* @param WP_REST_Response $response Response. |
| 52 |
* @param WC_Data $wcData Object. |
| 53 |
* |
| 54 |
* @return object |
| 55 |
*/ |
| 56 |
public function extendResponse( WP_REST_Response $response, WC_Data $wcData ): object { |
| 57 |
if ( ! $wcData instanceof WC_Order ) { |
| 58 |
return $response; |
| 59 |
} |
| 60 |
|
| 61 |
$responseData = $response->get_data(); |
| 62 |
if ( ! isset( $responseData['shipping_lines'] ) ) { |
| 63 |
return $response; |
| 64 |
} |
| 65 |
|
| 66 |
$order = $this->orderRepository->getByWcOrderWithValidCarrier( $wcData ); |
| 67 |
if ( $order === null ) { |
| 68 |
return $response; |
| 69 |
} |
| 70 |
|
| 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 ); |
| 76 |
} |
| 77 |
|
| 78 |
return $response; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Add Items to Shipping Lines. |
| 83 |
* |
| 84 |
* @param Order $order Packetery Order. |
| 85 |
* |
| 86 |
* @return array<string, string|null> |
| 87 |
*/ |
| 88 |
private function getPacketaItemsToShippingLines( Order $order ): array { |
| 89 |
$items = [ |
| 90 |
'carrier_id' => $order->getCarrier()->getId(), |
| 91 |
'point_id' => null, |
| 92 |
'point_name' => null, |
| 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; |
| 104 |
} |
| 105 |
} |
| 106 |
|