4
votes

Unfortunately, in my store all of the product quantities are in float. I used the below code to accept float value as a quantity in my website.

// Removes the WooCommerce filter, that is validating the quantity to be an int
remove_filter('woocommerce_stock_amount', 'intval');

// Add a filter, that validates the quantity to be a float
add_filter('woocommerce_stock_amount', 'floatval');

But, when I trying to create a order using API from my mobile app is not accepting the float value.

$woocommerce->post('orders', $getData);

anybody help me, how can I create a order using woocommerce api to accept float quantities.

1

1 Answers

4
votes

Unfortunately, you can't achieve this with the latest API version. You can try any of the solution given below but NOT recommended.

Solution 1: Use Legacy API.

Solution 2: Directly change 'type' => integer to 'type' => float (around 1190) in 'woocommerce/includes/api/class-wc-rest-orders-controller.php' which is responsible for the quantity (but note that you need to change every time you upgrade the plugin).

'quantity'     => array(
    'description' => __( 'Quantity ordered.', 'woocommerce' ),
    'type'        => 'float', // change here
    'context'     => array( 'view', 'edit' ),
),