Skip to content

Commit

Permalink
add API fix for weird json_encode float rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed May 17, 2024
1 parent c2531c4 commit 5e2de27
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 15 deletions.
37 changes: 37 additions & 0 deletions assets/img/animated/drawn-letters.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions assets/img/animated/pulsing-letters.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions assets/img/animated/slide-down.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions assets/img/wcpos-icon-animated.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion assets/img/wcpos-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 17 additions & 10 deletions includes/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ public function __construct() {
// These filters allow changes to the WC REST API response
add_filter( 'rest_dispatch_request', array( $this, 'rest_dispatch_request' ), 10, 4 );
add_filter( 'rest_pre_dispatch', array( $this, 'rest_pre_dispatch' ), 10, 3 );

$this->prevent_messages();
}

/**
Expand Down Expand Up @@ -322,6 +320,23 @@ public function rest_dispatch_request( $dispatch_result, $request, $route, $hand
// Check if the controller object is one of our registered controllers.
foreach ( $this->controllers as $key => $wcpos_controller ) {
if ( $controller === $wcpos_controller ) {
/**
* I'm adding some additional PHP settings before the response. Placing them here so they only apply to the POS API.
*
* - error_reporting(0) - Turn off error reporting
* - ini_set('display_errors', 0) - Turn off error display
* - ini_set('precision', 10) - Set the precision of floating point numbers
* - ini_set('serialize_precision', 10) - Set the precision of floating point numbers for serialization
*
* This is to prevent any PHP errors from being displayed in the response.
*
* The precision settings are to prevent floating point weirdness, eg: stock_quantity 3.6 becomes 3.6000000000000001
*/
error_reporting( 0 );
@ini_set( 'display_errors', 0 );
@ini_set( 'precision', 10 );
@ini_set( 'serialize_precision', 10 );

// Check if the controller has a 'wcpos_dispatch_request' method.
if ( method_exists( $controller, 'wcpos_dispatch_request' ) ) {
return $controller->wcpos_dispatch_request( $dispatch_result, $request, $route, $handler );
Expand All @@ -334,14 +349,6 @@ public function rest_dispatch_request( $dispatch_result, $request, $route, $hand
return $dispatch_result;
}

/**
* Error messages and notices can cause the JSON response to fail.
*/
private function prevent_messages(): void {
error_reporting( 0 );
@ini_set( 'display_errors', 0 );
}

/**
* Check the Authorization header for a Bearer token.
*
Expand Down
12 changes: 10 additions & 2 deletions includes/API/Orders_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public function __construct() {
*/
public function wcpos_dispatch_request( $dispatch_result, WP_REST_Request $request, $route, $handler ) {
$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();

add_filter( 'woocommerce_rest_prepare_shop_order_object', array( $this, 'wcpos_order_response' ), 10, 3 );
add_filter( 'woocommerce_order_get_items', array( $this, 'wcpos_order_get_items' ), 10, 3 );
Expand Down Expand Up @@ -283,14 +285,20 @@ public function prepare_line_items( $posted, $action = 'create', $item = null )

/**
* Gets the product ID from posted ID.
* NOTE: This overrides the parent method to remove the sku check, some users have products duplicated SKUs.
*
* @throws WC_REST_Exception When SKU or ID is not valid.
* @param array $posted Request data.
* @param string $action 'create' to add line item or 'update' to update it.
*
* @return int
*/
public function get_product_id( $posted, $action = 'create' ) {
// If id = 0, ie: miscellaneaous product, just return 0.
if ( isset( $posted['id'] ) && 0 == $posted['id'] ) {
return 0;
}

// Bypass the sku check. Some users have products with duplicated SKUs, esp. variable/variations.
$data = $posted;
unset( $data['sku'] );
return parent::get_product_id( $data, $action );
Expand All @@ -300,7 +308,7 @@ public function get_product_id( $posted, $action = 'create' ) {
* Validate billing email.
* NOTE: we have removed the format check to allow empty email addresses.
*
* @param WP_REST_Request $request
* @param WP_REST_Request $request Full details about the request.
*
* @return bool|WP_Error
*/
Expand Down
3 changes: 2 additions & 1 deletion includes/API/Products_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function get_item_schema() {
if ( $this->wcpos_allow_decimal_quantities() &&
isset( $schema['properties']['stock_quantity'] ) &&
\is_array( $schema['properties']['stock_quantity'] ) ) {
$schema['properties']['stock_quantity']['type'] = 'string';
$schema['properties']['stock_quantity']['type'] = 'float';
}

return $schema;
Expand Down Expand Up @@ -233,6 +233,7 @@ public function wcpos_product_response( WP_REST_Response $response, WC_Product $
* @param bool $creating True when creating object, false when updating.
*/
public function wcpos_insert_product_object( WC_Data $object, WP_REST_Request $request, $creating ): void {
// Update the barcode if it is set in the request.
$barcode_field = $this->wcpos_get_barcode_field();
if ( $request->has_param( 'barcode' ) ) {
$barcode = $request->get_param( 'barcode' );
Expand Down
1 change: 0 additions & 1 deletion includes/Templates/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ function ( $store ) {
),
'wp_credentials' => $auth_service->get_user_data( $user ),
'stores' => $stores,
'store_id' => isset( $_GET['store'] ) ? $_GET['store'] : null,
);

/**
Expand Down

0 comments on commit 5e2de27

Please sign in to comment.