【问题标题】:How to Remove Data from WC API Endpoint Request?如何从 WC API 端点请求中删除数据?
【发布时间】:2019-10-02 16:53:41
【问题描述】:

我正在将订单数据发送到第三方履行软件。它们默认与 WooCommerce REST API 集成。但是,最近对我的网站和订单数据的一些更改添加了额外的订单元数据。现在,当抓取相同数量的订单时,请求总是以 504 超时。请求现在大得不合理,为了解决这个问题,我决定通过减少请求产生的不相关和不必要的数据来进行优化。此外,我必须能够一次处理 100 个,我不能减少过滤器限制,它由 3rd 方应用程序自动设置。

问题中的端点

wc-api/v2/orders?status=processing&page=1&filter%5Blimit%5D=100

此端点抓取前 100 个正在处理的订单并将它们显示为一段 JSON。

要删除的内容

  1. customer_user_agent
  2. 头像_url
  3. cogs_cost
  4. cogs_total_cost

示例响应

{
   "orders":[
      {
         "id":137314,
         "order_number":"137314",
         "created_at":"2019-09-18T18:37:06Z",
         "updated_at":"2019-09-18T18:37:07Z",
         "completed_at":"1970-01-01T00:00:00Z",
         "status":"processing",
         "currency":"USD",
         "total":"49.50",
         "subtotal":"55.00",
         "total_line_items_quantity":1,
         "total_tax":"0.00",
         "total_shipping":"0.00",
         "cart_tax":"0.00",
         "shipping_tax":"0.00",
         "total_discount":"0.00",
         "shipping_methods":"Free shipping",
         "payment_details":{
            "method_id":"nmipay",
            "method_title":"Pay with Credit Card",
            "paid":true
         },
         "billing_address":{
            "first_name":"XXX",
            "last_name":"XXXX",
            "company":"",
            "address_1":"XXXX",
            "address_2":"",
            "city":"XXXX",
            "state":"XX",
            "postcode":"XXXXX",
            "country":"US",
            "email":"XXXXXX",
            "phone":"XXXX"
         },
         "shipping_address":{
            "first_name":"XXX",
            "last_name":"XX",
            "company":"",
            "address_1":"XXXXX",
            "address_2":"",
            "city":"XXX",
            "state":"XXX",
            "postcode":"XXX",
            "country":"XXXX"
         },
         "note":"",
         "customer_ip":"98.216.25.236",
         "customer_user_agent":"mozilla\/5.0 (iphone; cpu iphone os 12_4_1 like mac os x) applewebkit\/605.1.15 (khtml, like gecko) version\/12.1.2 mobile\/15e148 safari\/604.1",
         "customer_id":127116,
         "view_order_url":"XXXXX",
         "line_items":[
            {
               "id":198261,
               "subtotal":"55.00",
               "subtotal_tax":"0.00",
               "total":"55.00",
               "total_tax":"0.00",
               "price":"55.00",
               "quantity":1,
               "tax_class":"",
               "name":"Core Hoodie - Black, Large",
               "product_id":351,
               "sku":"ss-hoodie-core-zip-blk-lg",
               "meta":[

               ],
               "bundled_by":"",
               "bundled_item_title":"",
               "bundled_items":[

               ],
               "cogs_cost":"23.15",
               "cogs_total_cost":"23.15"
            }
         ],
         "shipping_lines":[
            {
               "id":198263,
               "method_id":"free_shipping",
               "method_title":"Free shipping",
               "total":"0.00"
            }
         ],
         "tax_lines":[

         ],
         "fee_lines":[
            {
               "id":198262,
               "title":"VIP Discount",
               "tax_class":"0",
               "total":"-5.50",
               "total_tax":"0.00"
            }
         ],
         "coupon_lines":[

         ],
         "cogs_total_cost":"23.15"
      }
   ]
}

这是我得到的最远的

我找到了以下钩子,但无法触发任何东西。

woocommerce_rest_prepare_shop_order_object
woocommerce_rest_prepare_shop_order
function remove_user_agent_from_rest_api( $response, $object, $request ) {
    unset($response->data['customer_user_agent']);
    return $response;
}

function test_rest_api() {
add_filter( "woocommerce_rest_pre_insert_shop_order", "remove_user_agent_from_rest_api", 10, 2 );
add_filter( "woocommerce_rest_pre_insert_shop_order_object", "remove_user_agent_from_rest_api", 10, 2 );
}

add_action( 'rest_api_init', 'test_rest_api', 0 );

这是性能调整问题吗?

这是来自 new relic 的示例跟踪和来自我的 NGINX 错误日志的示例。我可以调整什么来让服务器保持足够长的时间来处理这个请求。

2019/10/02 10:59:25 [error] 10270#10270: *5 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: XXX, server: X.net, request: "GET /?km_source=blog HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "X.net", referrer: "https://www.X.net/"
2019/10/02 11:00:42 [error] 10270#10270: *34 upstream timed out (110: Connection timed out) while reading response header from upstream, client: XXX, server: XXX.net, request: "GET /wc-api/v2/orders?status=processing&page=10&filter%5Blimit%5D=100&consumer_key=ck_XXX&consumer_secret=cs_XXX HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock", host: "X.net"
2019/10/02 11:07:53 [error] 13021#13021: *62 upstream timed out (110: Connection timed out) while reading response header from upstream, client: XXX, server: XXX.net, request: "GET /wc-api/v2/orders?status=processing&page=1&filter%5Blimit%5D=100&consumer_key=ck_XXX&consumer_secret=cs_XXX HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock", host: "X.net"
2019/10/02 11:13:45 [error] 15270#15270: *66 upstream timed out (110: Connection timed out) while reading response header from upstream, client: XXX, server: XXX.net, request: "GET /wc-api/v2/orders?status=processing&page=1&filter%5Blimit%5D=100&consumer_key=ck_XXX&consumer_secret=cs_XXX HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock", host: "XXX.net"
2019/10/02 11:15:44 [error] 16010#16010: *79 upstream timed out (110: Connection timed out) while reading response header from upstream, client: XXX, server: X.net, request: "GET /wc-api/v2/orders?status=processing&page=1&filter%5Blimit%5D=100&consumer_key=ck_XXX&consumer_secret=cs_XXX HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock", host: "X.net"

【问题讨论】:

    标签: wordpress woocommerce woocommerce-rest-api


    【解决方案1】:

    我注意到第一个问题是您的过滤器只传递了 2 个变量,而它们应该传递 3 个。

    add_filter( "woocommerce_rest_pre_insert_shop_order", "remove_user_agent_from_rest_api", 10, 3 );

    应该这样做。

    【讨论】:

      猜你喜欢
      • 2020-01-29
      • 2021-02-08
      • 1970-01-01
      • 2016-07-30
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多