namespace, '/orders', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_orders' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => $this->get_orders_params(), ), ) ); // Single order. register_rest_route( $this->namespace, '/orders/(?P[\d]+)', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_order' ), 'permission_callback' => array( $this, 'check_order_permission' ), 'args' => array( 'id' => array( 'description' => __( 'Order ID.', 'advanced-customer-account' ), 'type' => 'integer', 'required' => true, 'sanitize_callback' => 'absint', ), ), ), ) ); // Reorder. register_rest_route( $this->namespace, '/orders/(?P[\d]+)/reorder', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'reorder' ), 'permission_callback' => array( $this, 'check_order_permission' ), 'args' => array( 'id' => array( 'description' => __( 'Order ID.', 'advanced-customer-account' ), 'type' => 'integer', 'required' => true, 'sanitize_callback' => 'absint', ), ), ), ) ); // Order notes. register_rest_route( $this->namespace, '/orders/(?P[\d]+)/notes', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_order_notes' ), 'permission_callback' => array( $this, 'check_order_permission' ), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'add_order_note' ), 'permission_callback' => array( $this, 'check_order_permission' ), 'args' => array( 'note' => array( 'description' => __( 'Note content.', 'advanced-customer-account' ), 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_textarea_field', ), ), ), ) ); } /** * Get orders params. * * @return array */ private function get_orders_params() { return array_merge( $this->get_pagination_params(), array( 'status' => array( 'description' => __( 'Filter by order status.', 'advanced-customer-account' ), 'type' => 'string', 'default' => 'any', 'sanitize_callback' => 'sanitize_text_field', ), 'search' => array( 'description' => __( 'Search orders.', 'advanced-customer-account' ), 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), ) ); } /** * Check if user can access the order. * * @param WP_REST_Request $request Request object. * @return bool|WP_Error */ public function check_order_permission( $request ) { $permission = $this->check_permission( $request ); if ( is_wp_error( $permission ) ) { return $permission; } $order_id = $request->get_param( 'id' ); $order = wc_get_order( $order_id ); if ( ! $order ) { return new WP_Error( 'rest_order_not_found', esc_html__( 'Order not found.', 'advanced-customer-account' ), array( 'status' => 404 ) ); } // Check if user owns the order or is admin. if ( $order->get_customer_id() !== $this->get_customer_id() && ! current_user_can( 'manage_woocommerce' ) ) { return new WP_Error( 'rest_forbidden', esc_html__( 'You do not have permission to access this order.', 'advanced-customer-account' ), array( 'status' => 403 ) ); } return true; } /** * Get orders. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_orders( $request ) { $customer_id = $this->get_customer_id(); $page = $request->get_param( 'page' ); $per_page = $request->get_param( 'per_page' ); $status = $request->get_param( 'status' ); $search = $request->get_param( 'search' ); $args = array( 'customer_id' => $customer_id, 'limit' => $per_page, 'page' => $page, 'orderby' => 'date', 'order' => 'DESC', ); if ( 'any' !== $status ) { $args['status'] = $status; } // Get total count for pagination. $count_args = $args; $count_args['limit'] = -1; $count_args['return'] = 'ids'; $total_orders = count( wc_get_orders( $count_args ) ); // Get orders. $orders = wc_get_orders( $args ); $data = array(); foreach ( $orders as $order ) { $data[] = $this->prepare_order_response( $order ); } return $this->prepare_paginated_response( $data, $total_orders, $page, $per_page ); } /** * Get single order. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_order( $request ) { $order_id = $request->get_param( 'id' ); $order = wc_get_order( $order_id ); $data = $this->prepare_order_response( $order, true ); return rest_ensure_response( $data ); } /** * Reorder items from a previous order. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function reorder( $request ) { $order_id = $request->get_param( 'id' ); $order = wc_get_order( $order_id ); $added_items = array(); $failed_items = array(); foreach ( $order->get_items() as $item ) { $product_id = $item->get_product_id(); $quantity = $item->get_quantity(); $product = wc_get_product( $product_id ); if ( ! $product || ! $product->is_purchasable() || ! $product->is_in_stock() ) { $failed_items[] = array( 'product_id' => $product_id, 'product_name' => $item->get_name(), 'reason' => $product ? __( 'Out of stock', 'advanced-customer-account' ) : __( 'Product not found', 'advanced-customer-account' ), ); continue; } // Add to cart. $variation_id = $item->get_variation_id(); $cart_item_key = WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $item->get_meta_data() ); if ( $cart_item_key ) { $added_items[] = array( 'product_id' => $product_id, 'product_name' => $item->get_name(), 'quantity' => $quantity, ); } else { $failed_items[] = array( 'product_id' => $product_id, 'product_name' => $item->get_name(), 'reason' => __( 'Could not add to cart', 'advanced-customer-account' ), ); } } return rest_ensure_response( array( 'success' => ! empty( $added_items ), 'added' => $added_items, 'failed' => $failed_items, 'cart_url' => wc_get_cart_url(), 'checkout_url' => wc_get_checkout_url(), 'message' => empty( $failed_items ) ? __( 'All items added to cart!', 'advanced-customer-account' ) : sprintf( /* translators: %1$d: added count, %2$d: failed count */ __( '%1$d items added, %2$d items could not be added.', 'advanced-customer-account' ), count( $added_items ), count( $failed_items ) ), ) ); } /** * Get order notes. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_order_notes( $request ) { $order_id = $request->get_param( 'id' ); $order = wc_get_order( $order_id ); $notes = wc_get_order_notes( array( 'order_id' => $order_id, 'type' => 'customer', ) ); $data = array(); foreach ( $notes as $note ) { $data[] = array( 'id' => $note->id, 'content' => $note->content, 'date' => $this->format_date( $note->date_created->date( 'Y-m-d H:i:s' ) ), 'author' => $note->added_by, ); } return rest_ensure_response( $data ); } /** * Add order note. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function add_order_note( $request ) { $order_id = $request->get_param( 'id' ); $note = $request->get_param( 'note' ); $order = wc_get_order( $order_id ); $note_id = $order->add_order_note( $note, 1, // Is customer note. true // Added by user. ); if ( ! $note_id ) { return new WP_Error( 'rest_note_failed', esc_html__( 'Failed to add note.', 'advanced-customer-account' ), array( 'status' => 500 ) ); } return rest_ensure_response( array( 'success' => true, 'note_id' => $note_id, 'message' => __( 'Note added successfully.', 'advanced-customer-account' ), ) ); } /** * Prepare order response. * * @param WC_Order $order Order object. * @param bool $detailed Whether to include detailed info. * @return array */ private function prepare_order_response( $order, $detailed = false ) { $data = array( 'id' => $order->get_id(), 'number' => $order->get_order_number(), 'status' => $order->get_status(), 'status_label' => wc_get_order_status_name( $order->get_status() ), 'date_created' => $this->format_date( $order->get_date_created()->date( 'Y-m-d H:i:s' ) ), 'date_modified' => $order->get_date_modified() ? $this->format_date( $order->get_date_modified()->date( 'Y-m-d H:i:s' ) ) : null, 'total' => $this->format_price( $order->get_total() ), 'total_raw' => (float) $order->get_total(), 'items_count' => $order->get_item_count(), 'payment_method' => $order->get_payment_method_title(), ); if ( $detailed ) { $data['items'] = $this->get_order_items( $order ); $data['billing_address'] = $this->get_formatted_address( $order, 'billing' ); $data['shipping_address'] = $this->get_formatted_address( $order, 'shipping' ); $data['subtotal'] = $this->format_price( $order->get_subtotal() ); $data['shipping_total'] = $this->format_price( $order->get_shipping_total() ); $data['tax_total'] = $this->format_price( $order->get_total_tax() ); $data['discount_total'] = $this->format_price( $order->get_discount_total() ); $data['shipping_method'] = $order->get_shipping_method(); $data['customer_note'] = $order->get_customer_note(); // Tracking info (if available). $tracking = $order->get_meta( '_tracking_number' ); if ( $tracking ) { $data['tracking'] = array( 'number' => $tracking, 'url' => $order->get_meta( '_tracking_url' ), ); } } return $data; } /** * Get order items. * * @param WC_Order $order Order object. * @return array */ private function get_order_items( $order ) { $items = array(); foreach ( $order->get_items() as $item_id => $item ) { $product = $item->get_product(); $items[] = array( 'id' => $item_id, 'product_id' => $item->get_product_id(), 'variation_id' => $item->get_variation_id(), 'name' => $item->get_name(), 'quantity' => $item->get_quantity(), 'subtotal' => $this->format_price( $item->get_subtotal() ), 'total' => $this->format_price( $item->get_total() ), 'image' => $product ? wp_get_attachment_image_url( $product->get_image_id(), 'woocommerce_thumbnail' ) : wc_placeholder_img_src(), 'permalink' => $product ? $product->get_permalink() : '', 'sku' => $product ? $product->get_sku() : '', 'meta' => $this->get_item_meta( $item ), ); } return $items; } /** * Get item meta data. * * @param WC_Order_Item_Product $item Order item. * @return array */ private function get_item_meta( $item ) { $meta = array(); foreach ( $item->get_formatted_meta_data() as $meta_id => $meta_item ) { $meta[] = array( 'key' => $meta_item->display_key, 'value' => wp_strip_all_tags( $meta_item->display_value ), ); } return $meta; } /** * Get formatted address. * * @param WC_Order $order Order object. * @param string $type Address type (billing/shipping). * @return array */ private function get_formatted_address( $order, $type = 'billing' ) { $get_method = "get_{$type}_address"; return array( 'formatted' => $order->get_formatted_billing_address() ?: __( 'N/A', 'advanced-customer-account' ), 'first_name' => $order->{"get_{$type}_first_name"}(), 'last_name' => $order->{"get_{$type}_last_name"}(), 'company' => $order->{"get_{$type}_company"}(), 'address_1' => $order->{"get_{$type}_address_1"}(), 'address_2' => $order->{"get_{$type}_address_2"}(), 'city' => $order->{"get_{$type}_city"}(), 'state' => $order->{"get_{$type}_state"}(), 'postcode' => $order->{"get_{$type}_postcode"}(), 'country' => $order->{"get_{$type}_country"}(), 'phone' => 'billing' === $type ? $order->get_billing_phone() : '', 'email' => 'billing' === $type ? $order->get_billing_email() : '', ); } }