abstract-customer-ability.php
3 months ago
connect-customer-to-wp-user.php
3 months ago
create-customer.php
3 months ago
delete-customer.php
3 months ago
get-customer-bookings.php
3 months ago
get-customer-by-email.php
3 months ago
get-customer-orders.php
3 months ago
get-customer.php
3 months ago
get-total-customers-count.php
3 months ago
list-customers.php
3 months ago
search-customers.php
3 months ago
update-customer.php
3 months ago
get-customer-orders.php
84 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class LatePointAbilityGetCustomerOrders extends LatePointAbstractCustomerAbility { |
| 7 | |
| 8 | protected function configure(): void { |
| 9 | $this->id = 'latepoint/get-customer-orders'; |
| 10 | $this->label = __( 'Get customer orders', 'latepoint' ); |
| 11 | $this->description = __( 'Returns all orders for a specific customer.', 'latepoint' ); |
| 12 | $this->permission = 'customer__view'; |
| 13 | $this->read_only = true; |
| 14 | } |
| 15 | |
| 16 | public function get_input_schema(): array { |
| 17 | return [ |
| 18 | 'type' => 'object', |
| 19 | 'properties' => array_merge( |
| 20 | self::pagination(), |
| 21 | [ |
| 22 | 'customer_id' => [ |
| 23 | 'type' => 'integer', |
| 24 | 'description' => __( 'Customer ID.', 'latepoint' ), |
| 25 | ], |
| 26 | ] |
| 27 | ), |
| 28 | 'required' => [ 'customer_id' ], |
| 29 | ]; |
| 30 | } |
| 31 | |
| 32 | public function get_output_schema(): array { |
| 33 | return [ |
| 34 | 'type' => 'object', |
| 35 | 'properties' => [ |
| 36 | 'orders' => [ |
| 37 | 'type' => 'array', |
| 38 | 'items' => [ 'type' => 'object' ], |
| 39 | ], |
| 40 | 'total' => [ 'type' => 'integer' ], |
| 41 | 'page' => [ 'type' => 'integer' ], |
| 42 | 'per_page' => [ 'type' => 'integer' ], |
| 43 | ], |
| 44 | ]; |
| 45 | } |
| 46 | |
| 47 | public function execute( array $args ) { |
| 48 | $customer = new OsCustomerModel( (int) $args['customer_id'] ); |
| 49 | if ( $customer->is_new_record() ) { |
| 50 | return new WP_Error( 'not_found', __( 'Customer not found.', 'latepoint' ), [ 'status' => 404 ] ); |
| 51 | } |
| 52 | |
| 53 | $page = max( 1, (int) ( $args['page'] ?? 1 ) ); |
| 54 | $per_page = min( 100, max( 1, (int) ( $args['per_page'] ?? 20 ) ) ); |
| 55 | $offset = ( $page - 1 ) * $per_page; |
| 56 | |
| 57 | $query = ( new OsOrderModel() )->where( [ 'customer_id' => $customer->id ] ); |
| 58 | $orders = ( clone $query ) |
| 59 | ->order_by( 'created_at DESC' ) |
| 60 | ->set_limit( $per_page ) |
| 61 | ->set_offset( $offset ) |
| 62 | ->get_results_as_models(); |
| 63 | $total = $query->count(); |
| 64 | |
| 65 | $serialized = array_map( |
| 66 | fn( OsOrderModel $o ) => [ |
| 67 | 'id' => (int) $o->id, |
| 68 | 'status' => $o->status ?? '', |
| 69 | 'total' => $o->total ?? 0, |
| 70 | 'customer_id' => (int) $o->customer_id, |
| 71 | 'created_at' => $o->created_at ?? '', |
| 72 | ], |
| 73 | $orders |
| 74 | ); |
| 75 | |
| 76 | return [ |
| 77 | 'orders' => $serialized, |
| 78 | 'total' => (int) $total, |
| 79 | 'page' => $page, |
| 80 | 'per_page' => $per_page, |
| 81 | ]; |
| 82 | } |
| 83 | } |
| 84 |