【问题标题】:WooCommerce: Limit the fields returned in the "Update a Product" APIWooCommerce:限制“更新产品”API 中返回的字段
【发布时间】:2016-08-28 00:27:08
【问题描述】:

是否可以限制 WooCommerce POST“更新多个产品”中返回的字段?默认情况下,API 返回每个更新产品的所有字段。我想减小返回的 JSON 的大小。

API 文档声明“您可以使用 fields 参数限制响应中返回的字段”。但是,该示例适用于 GET /wc-api/v3/products

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

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

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

【问题讨论】:

    标签: wordpress woocommerce woocommerce-rest-api


    【解决方案1】:

    如果您可以访问安装 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 2017-12-31
      • 1970-01-01
      相关资源
      最近更新 更多