Skip to content

Commit

Permalink
Add endpoint for order statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Dec 12, 2023
1 parent 335876d commit c81d167
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 39 deletions.
135 changes: 98 additions & 37 deletions includes/API/Orders_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
use WC_Order_Query;
use WC_REST_Orders_Controller;
use WCPOS\WooCommercePOS\Logger;
use const WCPOS\WooCommercePOS\PLUGIN_NAME;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;
use Automattic\WooCommerce\Utilities\OrderUtil;

use const WCPOS\WooCommercePOS\PLUGIN_NAME;

/**
* Orders controller class.
*
Expand Down Expand Up @@ -55,7 +56,55 @@ public function __construct() {
}

/**
* Add custom fields to the product schema.
* Register routes.
*/
public function register_routes() {
parent::register_routes();

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<order_id>[\d]+)/email',
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'wcpos_send_email' ),
'permission_callback' => array( $this, 'wcpos_send_email_permissions_check' ),
'args' => array_merge(
$this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
array(
'email' => array(
'type' => 'string',
'description' => __( 'Email address', 'woocommerce-pos' ),
'required' => true,
),
'save_to' => array(
'type' => 'string',
'description' => __( 'Save email to order', 'woocommerce-pos' ),
'required' => false,
),
)
),
),
'schema' => array(),
)
);

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/statuses',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'wcpos_get_order_statuses' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
),
'schema' => array( $this, 'wcpos_get_public_order_statuses_schema' ),
)
);
}

/**
* Add custom fields to the order schema.
*/
public function get_item_schema() {
$schema = parent::get_item_schema();
Expand Down Expand Up @@ -230,41 +279,6 @@ public function get_collection_params() {
return $params;
}

/**
* Add route for sending emails.
*/
public function register_routes(): void {
parent::register_routes();

register_rest_route(
$this->namespace,
'/orders/(?P<order_id>[\d]+)/email',
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'wcpos_send_email' ),
'permission_callback' => array( $this, 'wcpos_send_email_permissions_check' ),
'args' => array_merge(
$this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
array(
'email' => array(
'type' => 'string',
'description' => __( 'Email address', 'woocommerce-pos' ),
'required' => true,
),
'save_to' => array(
'type' => 'string',
'description' => __( 'Save email to order', 'woocommerce-pos' ),
'required' => false,
),
)
),
),
'schema' => array(),
)
);
}

/**
* Send order email, optionally add email address.
*
Expand Down Expand Up @@ -350,6 +364,53 @@ public function wcpos_dispatch_request( $dispatch_result, WP_REST_Request $reque
return $dispatch_result;
}

/**
*
*/
public function wcpos_get_order_statuses() {
$statuses = wc_get_order_statuses();
$formatted_statuses = array();

foreach ( $statuses as $status_key => $status_name ) {
// Remove the 'wc-' prefix from the status key
$status_id = 'wc-' === substr( $status_key, 0, 3 ) ? substr( $status_key, 3 ) : $status_key;

$formatted_statuses[] = array(
'id' => $status_id,
'name' => $status_name,
);
}

return rest_ensure_response( $formatted_statuses );
}

/**
*
*/
public function wcpos_get_public_order_statuses_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'order_status',
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'Unique identifier for the order status.', 'woocommerce-pos-pro' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'name' => array(
'description' => __( 'Display name of the order status.', 'woocommerce-pos-pro' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);

return $schema;
}

/**
* Register hooks to modify WC REST API response.
*
Expand Down
14 changes: 12 additions & 2 deletions includes/Templates/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,17 @@ public function get_template(): void {
// disable cache plugins
$this->no_cache();

// last chance before template is rendered
do_action( 'woocommerce_pos_template_redirect' );
// last chance before frontend template is rendered
do_action( 'woocommerce_pos_frontend_template_redirect' );

/**
* Deprecated action.
*
* @TODO remove in 1.5.0
*/
if ( has_action( 'woocommerce_pos_template_redirect' ) ) {
do_action_deprecated( 'woocommerce_pos_template_redirect', array(), 'Version_1.4.0', 'woocommerce_pos_frontend_template_redirect' );
}

// add head & footer actions
add_action( 'woocommerce_pos_head', array( $this, 'head' ) );
Expand Down Expand Up @@ -119,6 +128,7 @@ function ( $store ) {
),
'wp_credentials' => $auth_service->get_user_data( $user ),
'stores' => $stores,
'store_id' => isset( $_GET['store'] ) ? $_GET['store'] : null,
);

/**
Expand Down
3 changes: 3 additions & 0 deletions includes/Templates/Login.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
/**
* Login template
* NOTE: This is the modal login template, ie: JWT expired, not the web login
*
* @package WooCommercePOS
*/

namespace WCPOS\WooCommercePOS\Templates;
Expand Down
27 changes: 27 additions & 0 deletions tests/includes/API/Test_Orders_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function test_register_routes(): void {

// added by WooCommerce POS
$this->assertArrayHasKey( '/wcpos/v1/orders/(?P<order_id>[\d]+)/email', $routes );
$this->assertArrayHasKey( '/wcpos/v1/orders/statuses', $routes );
}

/**
Expand Down Expand Up @@ -584,4 +585,30 @@ public function test_order_save_line_item_with_null_parent_name(): void {
$this->assertEquals( 201, $response->get_status() );
$this->assertEquals( 'woocommerce-pos', $data['created_via'] );
}

/**
* Retrieve all order statuses.
*/
public function test_get_order_statuses(): void {
$request = $this->wp_rest_get_request( '/wcpos/v1/orders/statuses' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();

$this->assertEquals( 200, $response->get_status() );
$this->assertNotEmpty( $data );

// Ensure that the response is an array
$this->assertIsArray( $data );

// Check if each element in the array has the required structure
foreach ( $data as $status ) {
$this->assertIsArray( $status );
$this->assertArrayHasKey( 'id', $status );
$this->assertArrayHasKey( 'name', $status );

// Check if the 'id' and 'name' fields are strings
$this->assertIsString( $status['id'] );
$this->assertIsString( $status['name'] );
}
}
}

0 comments on commit c81d167

Please sign in to comment.