WooCommerce:限制 "Update a Product" API 中返回的字段

WooCommerce: Limit the fields returned in the "Update a Product" API

是否可以限制 WooCommerce POST "Update Multiple Products" 中返回的字段?默认情况下 API returns 每个产品的所有字段都会更新。我想减小返回的 JSON 的大小。

API 文档指出 "You may limit the fields returned in the response using the fields parameter"。但是,示例是针对 GET /wc-api/v3/products

我需要限制 POST /wc-api/v3/products/bulk

的字段

我试过将 fields 参数附加到 URL,但它不起作用(参数被忽略并返回所有产品字段)。

我的 URL 如下所示: https://www.mywoocommercestore.com/wc-api/v3/products/bulk?fields=id,price,regular_price,sale_price,stock_quantity,error

如果您可以访问安装了 WooCommerce 的站点,那么您可以使用 woocommerce_api_products_bulk_response 过滤器并修改输出。

将以下代码添加到主题的 functions.php 文件

add_filter( 'woocommerce_api_products_bulk_response', 'custom_woocommerce_api_products_bulk_response' );

function custom_woocommerce_api_products_bulk_response( $products ) {

    // $products is an array containing all the data about the products
    // that were created or updated. Write your logic to remove unwanted fields

    return $products;
}