Skip to content

Commit

Permalink
fix decimal quantities for orders
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jan 16, 2024
1 parent 046380e commit 42554a9
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/update-pot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
if: steps.git-diff.outputs.changes == 'true'
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: 'chore(l10n): update languages/woocommerce-pos.pot'
commit_message: 'chore(i18n): update languages/woocommerce-pos.pot'
file_pattern: '*.pot'


3 changes: 2 additions & 1 deletion includes/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function __construct() {
return;
}

if ( 'heartbeat' == $_POST['action'] ) {
// ignore for WP Admin heartbeat requests
if ( isset( $_POST['action'] ) && 'heartbeat' == $_POST['action'] ) {
return;
}

Expand Down
9 changes: 8 additions & 1 deletion includes/API/Orders_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,17 @@ public function get_item_schema() {

// Modify line_items->parent_name to accept 'string' or 'null'
if ( isset( $schema['properties']['line_items'] ) &&
\is_array( $schema['properties']['line_items']['items']['properties'] ) ) {
\is_array( $schema['properties']['line_items']['items']['properties'] ) ) {
$schema['properties']['line_items']['items']['properties']['parent_name']['type'] = array( 'string', 'null' );
}

// Check for 'stock_quantity' and allow decimal
if ( $this->wcpos_allow_decimal_quantities() &&
isset( $schema['properties']['line_items'] ) &&
\is_array( $schema['properties']['line_items']['items']['properties'] ) ) {
$schema['properties']['line_items']['items']['properties']['quantity']['type'] = array( 'number' );
}

return $schema;
}

Expand Down
19 changes: 0 additions & 19 deletions includes/API/Traits/Product_Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,4 @@ public function wcpos_pos_only_products_enabled() {
// make sure it's true, just in case there's a corrupt setting
return true === $pos_only_products_enabled;
}

/**
* Get barcode field from settings.
*
* @return bool
*/
public function wcpos_allow_decimal_quantities() {
$allow_decimal_quantities = woocommerce_pos_get_settings( 'general', 'decimal_qty' );

// Check for WP_Error
if ( is_wp_error( $allow_decimal_quantities ) ) {
Logger::log( 'Error retrieving decimal_qty: ' . $allow_decimal_quantities->get_error_message() );

return false;
}

// make sure it's true, just in case there's a corrupt setting
return true === $allow_decimal_quantities;
}
}
28 changes: 25 additions & 3 deletions includes/API/Traits/WCPOS_REST_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ public function wcpos_format_id( string $id ): object {
* @return array
*/
public function wcpos_parse_meta_data( WC_Data $object ): array {
return array_map( function( $meta_data ) {
return $meta_data->get_data();
}, $object->get_meta_data());
return array_map(
function ( $meta_data ) {
return $meta_data->get_data();
},
$object->get_meta_data()
);
}

/**
Expand All @@ -50,4 +53,23 @@ public function wcpos_log_large_rest_response( WP_REST_Response $response, int $
Logger::log( "ID {$id} has a response size of {$response_size} bytes, exceeding the limit of {$max_response_size} bytes." );
}
}

/**
* Get barcode field from settings.
*
* @return bool
*/
public function wcpos_allow_decimal_quantities() {
$allow_decimal_quantities = woocommerce_pos_get_settings( 'general', 'decimal_qty' );

// Check for WP_Error
if ( is_wp_error( $allow_decimal_quantities ) ) {
Logger::log( 'Error retrieving decimal_qty: ' . $allow_decimal_quantities->get_error_message() );

return false;
}

// make sure it's true, just in case there's a corrupt setting
return true === $allow_decimal_quantities;
}
}
2 changes: 0 additions & 2 deletions includes/Orders.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* WCPOS Orders Class
* Extends WooCommerce Orders.
Expand All @@ -21,7 +20,6 @@ public function __construct() {
add_filter( 'woocommerce_valid_order_statuses_for_payment', array( $this, 'valid_order_statuses_for_payment' ), 10, 2 );
add_filter( 'woocommerce_valid_order_statuses_for_payment_complete', array( $this, 'valid_order_statuses_for_payment_complete' ), 10, 2 );
add_filter( 'woocommerce_payment_complete_order_status', array( $this, 'payment_complete_order_status' ), 10, 3 );

add_filter( 'woocommerce_hidden_order_itemmeta', array( $this, 'hidden_order_itemmeta' ) );

// order emails
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wcpos/woocommerce-pos",
"version": "1.4.5.1",
"version": "1.4.6",
"description": "A simple front-end for taking WooCommerce orders at the Point of Sale.",
"main": "index.js",
"workspaces": {
Expand Down
5 changes: 3 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: kilbot
Tags: cart, e-commerce, ecommerce, inventory, point-of-sale, pos, sales, sell, shop, shopify, store, vend, woocommerce, wordpress-ecommerce
Requires at least: 5.6 & WooCommerce 5.3
Tested up to: 6.4
Stable tag: 1.4.5.1
Stable tag: 1.4.6
License: GPL-3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html

Expand Down Expand Up @@ -63,7 +63,8 @@ There is more information on our website at [https://wcpos.com](https://wcpos.co

== Changelog ==

= 1.4.6 - 2024/01/XX =
= 1.4.6 - 2024/01/16 =
* Fix: decimal quantity for orders
* Fix: load translation files

= 1.4.5 - 2024/01/14 =
Expand Down
26 changes: 26 additions & 0 deletions tests/includes/API/Test_Orders_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,4 +775,30 @@ public function test_order_search_by_billing_first_name_with_excludes() {
$ids = wp_list_pluck( $data, 'id' );
$this->assertEquals( array( $order2->get_id() ), $ids );
}

/**
*
*/
public function test_create_order_with_decimal_quantity() {
$this->setup_decimal_quantity_tests();

$request = $this->wp_rest_post_request( '/wcpos/v1/orders' );
$request->set_body_params(
array(
'payment_method' => 'pos_cash',
'line_items' => array(
array(
'product_id' => 1,
'quantity' => '1.5',
),
),
)
);

$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 201, $response->get_status() );

$this->assertEquals( 'woocommerce-pos', $data['created_via'] );
}
}
2 changes: 1 addition & 1 deletion woocommerce-pos.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: WooCommerce POS
* Plugin URI: https://wordpress.org/plugins/woocommerce-pos/
* Description: A simple front-end for taking WooCommerce orders at the Point of Sale. Requires <a href="http://wordpress.org/plugins/woocommerce/">WooCommerce</a>.
* Version: 1.4.5.1
* Version: 1.4.6
* Author: kilbot
* Author URI: http://wcpos.com
* Text Domain: woocommerce-pos
Expand Down

0 comments on commit 42554a9

Please sign in to comment.