扩展Woocommerce Rest API类并更改/ orders端点的模式

问题描述

我正在调用woocommerce REST api创建一个数量为浮点值的订单 /wp-json/wc/v3/orders,但是REST api需要一个整数值,因此响应错误Response from the POST call below

我尝试在child-theme的functions.PHP中扩展Base API类,以覆盖要浮动的数量类型,但是那不起作用。我在这里想念什么?

class CUSTOM_WC_REST_Orders_Controller extends WC_REST_Orders_Controller
{

    public function get_item_schema()
    {
        $schema = parent::get_item_schema();
        $schema['properties']['line_items']['items']['properties']['quantity']['type'] = 'float';

        return $schema;
    }
}
new CUSTOM_WC_REST_Orders_Controller();

虽然我尝试使用相同的方法对产品api进行POST测试,但似乎可行。出于测试目的,我将regular_price属性字段更改为字符串,并且可以正常工作。以下代码供参考。

class CUSTOM_WC_REST_Product_Controller extends WC_REST_Products_Controller
{
    public function get_item_schema()
    {
        $schema = parent::get_item_schema();
        $schema["properties"]["regular_price"]["type"] = "string";
        return $schema;
    }
}

new CUSTOM_WC_REST_Product_Controller();

为什么扩展产品的REST Base类而不是订单有效?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)