Skip to content

Commit

Permalink
add API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jun 1, 2024
1 parent 3d453b0 commit d8f9976
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 10 deletions.
26 changes: 16 additions & 10 deletions includes/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,19 @@ public function register_routes() {
* @since 1.5.0
*
* @param array $controllers Associative array of controller identifiers to their corresponding class names.
* - 'auth' => Fully qualified name of the class handling authentication.
* - 'settings' => Fully qualified name of the class handling settings.
* - 'stores' => Fully qualified name of the class handling stores management.
* - 'products' => Fully qualified name of the class handling products.
* - 'product_variations' => Fully qualified name of the class handling product variations.
* - 'orders' => Fully qualified name of the class handling orders.
* - 'customers' => Fully qualified name of the class handling customers.
* - 'product_tags' => Fully qualified name of the class handling product tags.
* - 'product_categories' => Fully qualified name of the class handling product categories.
* - 'taxes' => Fully qualified name of the class handling taxes.
* - 'auth' => Fully qualified name of the class handling authentication.
* - 'settings' => Fully qualified name of the class handling settings.
* - 'stores' => Fully qualified name of the class handling stores management.
* - 'products' => Fully qualified name of the class handling products.
* - 'product_variations' => Fully qualified name of the class handling product variations.
* - 'orders' => Fully qualified name of the class handling orders.
* - 'customers' => Fully qualified name of the class handling customers.
* - 'product_tags' => Fully qualified name of the class handling product tags.
* - 'product_categories' => Fully qualified name of the class handling product categories.
* - 'taxes' => Fully qualified name of the class handling taxes.
* - 'shipping_methods' => Fully qualified name of the class handling shipping methods.
* - 'tax_classes' => Fully qualified name of the class handling tax classes.
* - 'order_statuses' => Fully qualified name of the class handling order statuses.
*/
$classes = apply_filters(
'woocommerce_pos_rest_api_controllers',
Expand All @@ -99,6 +102,9 @@ public function register_routes() {
'product_tags' => API\Product_Tags_Controller::class,
'product_categories' => API\Product_Categories_Controller::class,
'taxes' => API\Taxes_Controller::class,
'shipping_methods' => API\Shipping_Methods_Controller::class,
'tax_classes' => API\Tax_Classes_Controller::class,
'order_statuses' => API\Data_Order_Statuses_Controller::class,
)
);

Expand Down
138 changes: 138 additions & 0 deletions includes/API/Data_Order_Statuses_Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
* REST API Data controller.
*/

namespace WCPOS\WooCommercePOS\API;

\defined( 'ABSPATH' ) || die;

if ( ! class_exists( 'WC_REST_Data_Controller' ) ) {
return;
}

use WC_REST_Data_Controller;
use WP_REST_Server;
use WP_REST_Request;
use WP_Error;
use WP_REST_Response;

/**
* REST API Data controller.
*
* Seems overkill to create a new class for this, but WC doesn't provide a way to get the list of order statuses.
*/
class Data_Order_Statuses_Controller extends WC_REST_Data_Controller {

/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wcpos/v1';

/**
* Route base.
*
* @var string
*/
protected $rest_base = 'data/order_statuses';

/**
* Return the list of order statuses.
*
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$statuses = wc_get_order_statuses();
$data = array();

foreach ( $statuses as $status => $label ) {
// Remove the 'wc-' prefix from the status
$status = str_replace( 'wc-', '', $status );

$resource = array(
'status' => $status,
'label' => $label,
);

$item = $this->prepare_item_for_response( (object) $resource, $request );
$data[] = $this->prepare_response_for_collection( $item );
}

return rest_ensure_response( $data );
}

/**
* Prepare a data resource object for serialization.
*
* @param stdClass $resource Resource data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response Response data.
*/
public function prepare_item_for_response( $resource, $request ) {
$data = array(
'status' => $resource->status,
'label' => $resource->label,
);

$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, 'view' );

// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $resource ) );

return $response;
}

/**
* Prepare links for the request.
*
* @param object $item Data object.
* @return array Links for the given country.
*/
protected function prepare_links( $item ) {
$links = array(
// 'self' => array(
// 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $item->status ) ),
// ),
'collection' => array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
),
);

return $links;
}

/**
* Get the data index schema, conforming to JSON Schema.
*
* @since 3.5.0
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'data_index',
'type' => 'object',
'properties' => array(
'status' => array(
'description' => __( 'Order Status.', 'woocommerce-pos' ),
'type' => 'string',
'context' => array( 'view' ),
'readonly' => true,
),
'label' => array(
'description' => __( 'Order Status Label.', 'woocommerce-pos' ),
'type' => 'string',
'context' => array( 'view' ),
'readonly' => true,
),
),
);

return $this->add_additional_fields_schema( $schema );
}
}
7 changes: 7 additions & 0 deletions includes/API/Orders_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ public function __construct() {
* @param array $handler Route handler used for the request.
*/
public function wcpos_dispatch_request( $dispatch_result, WP_REST_Request $request, $route, $handler ) {
/**
* Force decimal rounding to 6 places for all order data. This matches the POS.
*
* @TODO - should this be flexible via a query param from the POS?
*/
$request->set_param( 'dp', '6' );

$this->wcpos_request = $request;
// set hpos_enabled again for tests to work @TODO - fix this
$this->hpos_enabled = class_exists( OrderUtil::class ) && OrderUtil::custom_orders_table_usage_is_enabled();
Expand Down
27 changes: 27 additions & 0 deletions includes/API/Shipping_Methods_Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* REST API WC Shipping Methods controller
*/

namespace WCPOS\WooCommercePOS\API;

\defined( 'ABSPATH' ) || die;

if ( ! class_exists( 'WC_REST_Shipping_Methods_Controller' ) ) {
return;
}

use WC_REST_Shipping_Methods_Controller;

/**
* Shipping methods controller class.
*/
class Shipping_Methods_Controller extends WC_REST_Shipping_Methods_Controller {

/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wcpos/v1';
}
27 changes: 27 additions & 0 deletions includes/API/Tax_Classes_Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* REST API Tax Classes controller class.
*/

namespace WCPOS\WooCommercePOS\API;

\defined( 'ABSPATH' ) || die;

if ( ! class_exists( 'WC_REST_Tax_Classes_Controller' ) ) {
return;
}

use WC_REST_Tax_Classes_Controller;

/**
* REST API Tax Classes controller class.
*/
class Tax_Classes_Controller extends WC_REST_Tax_Classes_Controller {

/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wcpos/v1';
}
27 changes: 27 additions & 0 deletions includes/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function __construct() {
add_filter( 'woocommerce_hidden_order_itemmeta', array( $this, 'hidden_order_itemmeta' ) );
add_filter( 'woocommerce_order_item_product', array( $this, 'order_item_product' ), 10, 2 );
add_filter( 'woocommerce_order_get_tax_location', array( $this, 'get_tax_location' ), 10, 2 );
add_action( 'woocommerce_order_item_after_calculate_taxes', array( $this, 'order_item_after_calculate_taxes' ) );
add_action( 'woocommerce_order_item_shipping_after_calculate_taxes', array( $this, 'order_item_after_calculate_taxes' ) );

// order emails
$admin_emails = array(
Expand Down Expand Up @@ -264,4 +266,29 @@ public function get_tax_location( $args, WC_Abstract_Order $order ) {

return $args;
}

/**
* Calculate taxes for an order item.
*
* @param WC_Order_Item|WC_Order_Item_Shipping $item Order item object.
*/
public function order_item_after_calculate_taxes( $item ) {
$meta_data = $item->get_meta_data();

foreach ( $meta_data as $meta ) {
foreach ( $meta_data as $meta ) {
if ( '_woocommerce_pos_data' === $meta->key ) {
$pos_data = json_decode( $meta->value, true );

if ( json_last_error() === JSON_ERROR_NONE ) {
if ( isset( $pos_data['tax_status'] ) && 'none' == $pos_data['tax_status'] ) {
$item->set_taxes( false );
}
} else {
Logger::log( 'JSON parse error: ' . json_last_error_msg() );
}
}
}
}
}
}

0 comments on commit d8f9976

Please sign in to comment.