首页 > 解决方案 > 为 /orders 端点扩展 Woocommerce Rest API 类和更改架构

问题描述

我正在调用 woocommerce REST api 来创建订单: /wp-json/wc/v3/orders数量为浮点值,但 REST api 需要一个整数数量值,因此响应错误。 来自下面的 POST 调用的响应

我尝试在子主题的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 基类而不是订单?

标签: woocommercewoocommerce-rest-api

解决方案


推荐阅读