Skip to content

Commit d8f9976

Browse files
committed
add API endpoints
1 parent 3d453b0 commit d8f9976

File tree

6 files changed

+242
-10
lines changed

6 files changed

+242
-10
lines changed

includes/API.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,19 @@ public function register_routes() {
7272
* @since 1.5.0
7373
*
7474
* @param array $controllers Associative array of controller identifiers to their corresponding class names.
75-
* - 'auth' => Fully qualified name of the class handling authentication.
76-
* - 'settings' => Fully qualified name of the class handling settings.
77-
* - 'stores' => Fully qualified name of the class handling stores management.
78-
* - 'products' => Fully qualified name of the class handling products.
79-
* - 'product_variations' => Fully qualified name of the class handling product variations.
80-
* - 'orders' => Fully qualified name of the class handling orders.
81-
* - 'customers' => Fully qualified name of the class handling customers.
82-
* - 'product_tags' => Fully qualified name of the class handling product tags.
83-
* - 'product_categories' => Fully qualified name of the class handling product categories.
84-
* - 'taxes' => Fully qualified name of the class handling taxes.
75+
* - 'auth' => Fully qualified name of the class handling authentication.
76+
* - 'settings' => Fully qualified name of the class handling settings.
77+
* - 'stores' => Fully qualified name of the class handling stores management.
78+
* - 'products' => Fully qualified name of the class handling products.
79+
* - 'product_variations' => Fully qualified name of the class handling product variations.
80+
* - 'orders' => Fully qualified name of the class handling orders.
81+
* - 'customers' => Fully qualified name of the class handling customers.
82+
* - 'product_tags' => Fully qualified name of the class handling product tags.
83+
* - 'product_categories' => Fully qualified name of the class handling product categories.
84+
* - 'taxes' => Fully qualified name of the class handling taxes.
85+
* - 'shipping_methods' => Fully qualified name of the class handling shipping methods.
86+
* - 'tax_classes' => Fully qualified name of the class handling tax classes.
87+
* - 'order_statuses' => Fully qualified name of the class handling order statuses.
8588
*/
8689
$classes = apply_filters(
8790
'woocommerce_pos_rest_api_controllers',
@@ -99,6 +102,9 @@ public function register_routes() {
99102
'product_tags' => API\Product_Tags_Controller::class,
100103
'product_categories' => API\Product_Categories_Controller::class,
101104
'taxes' => API\Taxes_Controller::class,
105+
'shipping_methods' => API\Shipping_Methods_Controller::class,
106+
'tax_classes' => API\Tax_Classes_Controller::class,
107+
'order_statuses' => API\Data_Order_Statuses_Controller::class,
102108
)
103109
);
104110

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
/**
3+
* REST API Data controller.
4+
*/
5+
6+
namespace WCPOS\WooCommercePOS\API;
7+
8+
\defined( 'ABSPATH' ) || die;
9+
10+
if ( ! class_exists( 'WC_REST_Data_Controller' ) ) {
11+
return;
12+
}
13+
14+
use WC_REST_Data_Controller;
15+
use WP_REST_Server;
16+
use WP_REST_Request;
17+
use WP_Error;
18+
use WP_REST_Response;
19+
20+
/**
21+
* REST API Data controller.
22+
*
23+
* Seems overkill to create a new class for this, but WC doesn't provide a way to get the list of order statuses.
24+
*/
25+
class Data_Order_Statuses_Controller extends WC_REST_Data_Controller {
26+
27+
/**
28+
* Endpoint namespace.
29+
*
30+
* @var string
31+
*/
32+
protected $namespace = 'wcpos/v1';
33+
34+
/**
35+
* Route base.
36+
*
37+
* @var string
38+
*/
39+
protected $rest_base = 'data/order_statuses';
40+
41+
/**
42+
* Return the list of order statuses.
43+
*
44+
* @param WP_REST_Request $request Request data.
45+
* @return WP_Error|WP_REST_Response
46+
*/
47+
public function get_items( $request ) {
48+
$statuses = wc_get_order_statuses();
49+
$data = array();
50+
51+
foreach ( $statuses as $status => $label ) {
52+
// Remove the 'wc-' prefix from the status
53+
$status = str_replace( 'wc-', '', $status );
54+
55+
$resource = array(
56+
'status' => $status,
57+
'label' => $label,
58+
);
59+
60+
$item = $this->prepare_item_for_response( (object) $resource, $request );
61+
$data[] = $this->prepare_response_for_collection( $item );
62+
}
63+
64+
return rest_ensure_response( $data );
65+
}
66+
67+
/**
68+
* Prepare a data resource object for serialization.
69+
*
70+
* @param stdClass $resource Resource data.
71+
* @param WP_REST_Request $request Request object.
72+
* @return WP_REST_Response $response Response data.
73+
*/
74+
public function prepare_item_for_response( $resource, $request ) {
75+
$data = array(
76+
'status' => $resource->status,
77+
'label' => $resource->label,
78+
);
79+
80+
$data = $this->add_additional_fields_to_object( $data, $request );
81+
$data = $this->filter_response_by_context( $data, 'view' );
82+
83+
// Wrap the data in a response object.
84+
$response = rest_ensure_response( $data );
85+
$response->add_links( $this->prepare_links( $resource ) );
86+
87+
return $response;
88+
}
89+
90+
/**
91+
* Prepare links for the request.
92+
*
93+
* @param object $item Data object.
94+
* @return array Links for the given country.
95+
*/
96+
protected function prepare_links( $item ) {
97+
$links = array(
98+
// 'self' => array(
99+
// 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $item->status ) ),
100+
// ),
101+
'collection' => array(
102+
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
103+
),
104+
);
105+
106+
return $links;
107+
}
108+
109+
/**
110+
* Get the data index schema, conforming to JSON Schema.
111+
*
112+
* @since 3.5.0
113+
* @return array
114+
*/
115+
public function get_item_schema() {
116+
$schema = array(
117+
'$schema' => 'http://json-schema.org/draft-04/schema#',
118+
'title' => 'data_index',
119+
'type' => 'object',
120+
'properties' => array(
121+
'status' => array(
122+
'description' => __( 'Order Status.', 'woocommerce-pos' ),
123+
'type' => 'string',
124+
'context' => array( 'view' ),
125+
'readonly' => true,
126+
),
127+
'label' => array(
128+
'description' => __( 'Order Status Label.', 'woocommerce-pos' ),
129+
'type' => 'string',
130+
'context' => array( 'view' ),
131+
'readonly' => true,
132+
),
133+
),
134+
);
135+
136+
return $this->add_additional_fields_schema( $schema );
137+
}
138+
}

includes/API/Orders_Controller.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ public function __construct() {
8080
* @param array $handler Route handler used for the request.
8181
*/
8282
public function wcpos_dispatch_request( $dispatch_result, WP_REST_Request $request, $route, $handler ) {
83+
/**
84+
* Force decimal rounding to 6 places for all order data. This matches the POS.
85+
*
86+
* @TODO - should this be flexible via a query param from the POS?
87+
*/
88+
$request->set_param( 'dp', '6' );
89+
8390
$this->wcpos_request = $request;
8491
// set hpos_enabled again for tests to work @TODO - fix this
8592
$this->hpos_enabled = class_exists( OrderUtil::class ) && OrderUtil::custom_orders_table_usage_is_enabled();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* REST API WC Shipping Methods controller
4+
*/
5+
6+
namespace WCPOS\WooCommercePOS\API;
7+
8+
\defined( 'ABSPATH' ) || die;
9+
10+
if ( ! class_exists( 'WC_REST_Shipping_Methods_Controller' ) ) {
11+
return;
12+
}
13+
14+
use WC_REST_Shipping_Methods_Controller;
15+
16+
/**
17+
* Shipping methods controller class.
18+
*/
19+
class Shipping_Methods_Controller extends WC_REST_Shipping_Methods_Controller {
20+
21+
/**
22+
* Endpoint namespace.
23+
*
24+
* @var string
25+
*/
26+
protected $namespace = 'wcpos/v1';
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* REST API Tax Classes controller class.
4+
*/
5+
6+
namespace WCPOS\WooCommercePOS\API;
7+
8+
\defined( 'ABSPATH' ) || die;
9+
10+
if ( ! class_exists( 'WC_REST_Tax_Classes_Controller' ) ) {
11+
return;
12+
}
13+
14+
use WC_REST_Tax_Classes_Controller;
15+
16+
/**
17+
* REST API Tax Classes controller class.
18+
*/
19+
class Tax_Classes_Controller extends WC_REST_Tax_Classes_Controller {
20+
21+
/**
22+
* Endpoint namespace.
23+
*
24+
* @var string
25+
*/
26+
protected $namespace = 'wcpos/v1';
27+
}

includes/Orders.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function __construct() {
3434
add_filter( 'woocommerce_hidden_order_itemmeta', array( $this, 'hidden_order_itemmeta' ) );
3535
add_filter( 'woocommerce_order_item_product', array( $this, 'order_item_product' ), 10, 2 );
3636
add_filter( 'woocommerce_order_get_tax_location', array( $this, 'get_tax_location' ), 10, 2 );
37+
add_action( 'woocommerce_order_item_after_calculate_taxes', array( $this, 'order_item_after_calculate_taxes' ) );
38+
add_action( 'woocommerce_order_item_shipping_after_calculate_taxes', array( $this, 'order_item_after_calculate_taxes' ) );
3739

3840
// order emails
3941
$admin_emails = array(
@@ -264,4 +266,29 @@ public function get_tax_location( $args, WC_Abstract_Order $order ) {
264266

265267
return $args;
266268
}
269+
270+
/**
271+
* Calculate taxes for an order item.
272+
*
273+
* @param WC_Order_Item|WC_Order_Item_Shipping $item Order item object.
274+
*/
275+
public function order_item_after_calculate_taxes( $item ) {
276+
$meta_data = $item->get_meta_data();
277+
278+
foreach ( $meta_data as $meta ) {
279+
foreach ( $meta_data as $meta ) {
280+
if ( '_woocommerce_pos_data' === $meta->key ) {
281+
$pos_data = json_decode( $meta->value, true );
282+
283+
if ( json_last_error() === JSON_ERROR_NONE ) {
284+
if ( isset( $pos_data['tax_status'] ) && 'none' == $pos_data['tax_status'] ) {
285+
$item->set_taxes( false );
286+
}
287+
} else {
288+
Logger::log( 'JSON parse error: ' . json_last_error_msg() );
289+
}
290+
}
291+
}
292+
}
293+
}
267294
}

0 commit comments

Comments
 (0)