PluginProbe
Packeta / 1.6.2
Packeta v1.6.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Order / ApiExtender.php

ApiExtender.php in Packeta 1.6.2, at src/Packetery/Module/Order/ApiExtender.php

112 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Exception\InvalidCarrierException;
14 use Packetery\Module\ShippingMethod;
15 use WC_Data;
16 use WC_Order;
17 use WP_REST_Response;
18
19 /**
20 * Class ApiExtender.
21 */
22 class ApiExtender {
23
24 /**
25 * Order repository.
26 *
27 * @var Repository
28 */
29 private $orderRepository;
30
31 /**
32 * Constructor.
33 *
34 * @param Repository $orderRepository Order repository.
35 */
36 public function __construct( Repository $orderRepository ) {
37 $this->orderRepository = $orderRepository;
38 }
39
40 /**
41 * Registers service.
42 *
43 * @return void
44 */
45 public function register(): void {
46 add_filter( 'woocommerce_rest_prepare_shop_order_object', [ $this, 'extendResponse' ], 20, 2 );
47 }
48
49 /**
50 * Extends WooCommerce API response with plugin data.
51 *
52 * @param WP_REST_Response $response Response.
53 * @param WC_Data $object Object.
54 *
55 * @return object
56 */
57 public function extendResponse( WP_REST_Response $response, WC_Data $object ): object {
58 if ( ! $object instanceof WC_Order ) {
59 return $response;
60 }
61
62 $responseData = $response->get_data();
63 if ( ! isset( $responseData['shipping_lines'] ) ) {
64 return $response;
65 }
66
67 try {
68 $order = $this->orderRepository->getByWcOrder( $object );
69 } catch ( InvalidCarrierException $invalidCarrierException ) {
70 return $response;
71 }
72
73 if ( null === $order ) {
74 return $response;
75 }
76
77 foreach ( $responseData['shipping_lines'] as $key => $shippingLine ) {
78 if ( ShippingMethod::PACKETERY_METHOD_ID !== $shippingLine['method_id'] ) {
79 continue;
80 }
81 $response->data['shipping_lines'][ $key ]['packeta'] = $this->getPacketaItemsToShippingLines( $order );
82 }
83
84 return $response;
85 }
86
87 /**
88 * Add Items to Shipping Lines.
89 *
90 * @param Order $order Packetery Order.
91 *
92 * @return array
93 */
94 private function getPacketaItemsToShippingLines( Order $order ): array {
95 $items = [
96 'carrier_id' => $order->getCarrier()->getId(),
97 'point_id' => null,
98 'point_name' => null,
99 ];
100
101 if ( null !== $order->getPickupPoint() ) {
102 $items['point_id'] = $order->getPickupPoint()->getId();
103 $items['point_name'] = $order->getPickupPoint()->getName();
104 }
105
106 $items['carrier_number'] = $order->getCarrierNumber();
107 $items['packet_id'] = $order->getPacketId();
108
109 return $items;
110 }
111 }
112