register( 'woo_orders_list', array( 'description' => 'List WooCommerce orders.', 'domain' => 'woocommerce', 'capability' => 'manage_woocommerce', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'status' => array( 'type' => 'string' ), 'per_page' => array( 'type' => 'integer', 'default' => 20 ), 'page' => array( 'type' => 'integer', 'default' => 1 ), ), ), ), function ( $args ) { $query_args = array( 'limit' => isset( $args['per_page'] ) ? min( 100, absint( $args['per_page'] ) ) : 20, 'page' => isset( $args['page'] ) ? max( 1, absint( $args['page'] ) ) : 1, 'return' => 'objects', ); if ( ! empty( $args['status'] ) ) { $query_args['status'] = sanitize_key( $args['status'] ); } $orders = wc_get_orders( $query_args ); $items = array_map( 'agentdesk_format_wc_order', $orders ); return array( 'orders' => $items ); } ); $registry->register( 'woo_orders_get', array( 'description' => 'Get a WooCommerce order by ID.', 'domain' => 'woocommerce', 'capability' => 'manage_woocommerce', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer' ) ), 'required' => array( 'id' ), ), ), function ( $args ) { $order_type = get_post_type_object( 'shop_order' ); if ( $order_type && ! current_user_can( $order_type->cap->edit_others_posts ) ) { return new WP_Error( 'agentdesk_forbidden', __( 'You are not allowed to manage orders.', 'agentdesk-site-operations' ), array( 'status' => 403 ) ); } $order = wc_get_order( absint( $args['id'] ) ); if ( ! $order ) { return new WP_Error( 'agentdesk_not_found', 'Order not found' ); } return agentdesk_format_wc_order( $order, true ); } ); $registry->register( 'woo_orders_update_status', array( 'description' => 'Update order status.', 'domain' => 'woocommerce', 'capability' => 'manage_woocommerce', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer' ), 'status' => array( 'type' => 'string' ), ), 'required' => array( 'id', 'status' ), ), ), function ( $args ) { $order_type = get_post_type_object( 'shop_order' ); if ( $order_type && ! current_user_can( $order_type->cap->edit_others_posts ) ) { return new WP_Error( 'agentdesk_forbidden', __( 'You are not allowed to manage orders.', 'agentdesk-site-operations' ), array( 'status' => 403 ) ); } $order = wc_get_order( absint( $args['id'] ) ); if ( ! $order ) { return new WP_Error( 'agentdesk_not_found', 'Order not found' ); } $order->update_status( sanitize_key( $args['status'] ) ); return agentdesk_format_wc_order( $order, true ); } ); $registry->register( 'woo_orders_add_note', array( 'description' => 'Add a note to an order.', 'domain' => 'woocommerce', 'capability' => 'manage_woocommerce', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer' ), 'note' => array( 'type' => 'string' ), 'customer_note' => array( 'type' => 'boolean', 'default' => false ), ), 'required' => array( 'id', 'note' ), ), ), function ( $args ) { $order_type = get_post_type_object( 'shop_order' ); if ( $order_type && ! current_user_can( $order_type->cap->edit_others_posts ) ) { return new WP_Error( 'agentdesk_forbidden', __( 'You are not allowed to manage orders.', 'agentdesk-site-operations' ), array( 'status' => 403 ) ); } $order = wc_get_order( absint( $args['id'] ) ); if ( ! $order ) { return new WP_Error( 'agentdesk_not_found', 'Order not found' ); } $note_id = $order->add_order_note( sanitize_textarea_field( $args['note'] ), ! empty( $args['customer_note'] ) ); return array( 'order_id' => $order->get_id(), 'note_id' => $note_id ); } ); $registry->register( 'woo_customers_list', array( 'description' => 'List WooCommerce customers.', 'domain' => 'woocommerce', 'capability' => 'manage_woocommerce', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'search' => array( 'type' => 'string' ), 'per_page' => array( 'type' => 'integer', 'default' => 20 ), ), ), ), function ( $args ) { $query = new WP_User_Query( array( 'role' => 'customer', 'search' => isset( $args['search'] ) ? '*' . sanitize_text_field( $args['search'] ) . '*' : '', 'number' => isset( $args['per_page'] ) ? min( 100, absint( $args['per_page'] ) ) : 20, ) ); $items = array(); foreach ( $query->get_results() as $user ) { $items[] = array( 'id' => $user->ID, 'email' => $user->user_email, 'name' => $user->display_name, ); } return array( 'customers' => $items, 'total' => (int) $query->get_total() ); } ); $registry->register( 'woo_reports_sales', array( 'description' => 'Get WooCommerce sales totals for a period.', 'domain' => 'woocommerce', 'capability' => 'manage_woocommerce', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'period' => array( 'type' => 'string', 'enum' => array( 'week', 'month', 'year' ), 'default' => 'month' ), ), ), ), function ( $args ) { $period = isset( $args['period'] ) ? sanitize_key( $args['period'] ) : 'month'; $orders = wc_get_orders( array( 'status' => array( 'wc-completed', 'wc-processing' ), 'date_created' => '>' . gmdate( 'Y-m-d', strtotime( '-1 ' . $period ) ), 'limit' => -1, 'return' => 'ids', ) ); $total = 0; foreach ( $orders as $order_id ) { $order = wc_get_order( $order_id ); if ( $order ) { $total += (float) $order->get_total(); } } return array( 'period' => $period, 'order_count' => count( $orders ), 'total_sales' => $total, 'currency' => get_woocommerce_currency(), ); } ); } ); /** * Format WC order. * * @param WC_Order $order Order. * @param bool $full Full details. * @return array */ function agentdesk_format_wc_order( $order, $full = false ) { $data = array( 'id' => $order->get_id(), 'status' => $order->get_status(), 'total' => $order->get_total(), 'currency' => $order->get_currency(), 'date' => $order->get_date_created() ? $order->get_date_created()->date( 'Y-m-d H:i:s' ) : null, ); if ( $full ) { $data['billing'] = $order->get_address( 'billing' ); $data['shipping'] = $order->get_address( 'shipping' ); $data['items'] = array(); foreach ( $order->get_items() as $item ) { $data['items'][] = array( 'name' => $item->get_name(), 'quantity' => $item->get_quantity(), 'total' => $item->get_total(), ); } } return $data; }