Woocommerce API - 创建具有浮动数量的订单

Sat*_*mar 4 wordpress woocommerce wordpress-rest-api woocommerce-rest-api

不幸的是,在我的商店里,所有产品的数量都是浮动的。我使用下面的代码接受浮点值作为我网站中的数量。

// 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');
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用移动应用程序中的 API 创建订单时,不接受浮点值。

$woocommerce->post('orders', $getData);
Run Code Online (Sandbox Code Playgroud)

有人帮助我,我如何使用 woocommerce api 创建订单来接受浮动数量。

Out*_*ess 5

不幸的是,您无法使用最新的 API 版本来实现这一点。您可以尝试下面给出的任何解决方案,但不推荐

解决方案 1:使用旧版 API

方案二:直接改成'type' => integerwoocommerce 'type' => float/includes/api/class-wc-rest-orders-controller.php 中负责数量的(1190左右)(但注意每次升级插件时都需要改) )。

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