如何从WC API端点请求中删除数据?

时间:2019-10-02 16:53:41

标签: wordpress woocommerce woocommerce-rest-api

我正在将订单数据运送到第三方履行软件。它们默认情况下与WooCommerce REST API集成。但是,最近对我的站点和订单数据所做的一些更改增加了其他订单元。现在,当获取数量总是与504相同的请求时,请求超时。请求现在变得不合理,为了解决此问题,我决定通过减少请求产生的无关数据和不必要数据来进行优化。另外,我必须能够一次处理100个我不能降低的过滤器限制,该限制是由第三方应用程序自动设置的。

问题端点

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

此端点获取处理中的前100个订单并将其显示为JSON。

要删除的东西

  1. customer_user_agent
  2. avatar_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 );

这是性能调整问题吗?

这是来自新遗物的样本跟踪,以及来自我的NGINX错误日志的样本。我该如何调整以保持服务器打开足够长的时间来处理此请求。

enter image description here

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"

1 个答案:

答案 0 :(得分:0)

第一个问题是,您的过滤器只应传递2个变量,而应传递3个变量。

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

应该这样做。