自定义JSON格式webhook WooCommerce Wordpress

时间:2018-03-11 17:54:55

标签: json wordpress woocommerce webhooks

我希望我的Woocommerce商店连接到一个运输包裹公司。没有现有的WooCommerce插件。我想用webhooks来做,我已经在order.created动作之后成功地向URL发出了POST请求。 JSON输出包含所有送货信息。但我想改变JSON输出的格式。

当前的JSON输出是这样的。



{
  "id": 605,
  "parent_id": 0,
  "status": "pending",
  "order_key": "wc_order_5893614a8fb78",
  "number": 605,
  "currency": "USD",
  "version": "2.6.13",
  "prices_include_tax": false,
  "date_created": "2017-02-02T16:41:46",
  "date_modified": "2017-02-02T16:41:46",
  "customer_id": 2,
  "discount_total": "0.00",
  "discount_tax": "0.00",
  "shipping_total": "13.60",
  "shipping_tax": "1.22",
  "cart_tax": "1.44",
  "total": "32.24",
  "total_tax": "2.66",
  "billing": {
    "first_name": "Damandeep",
    "last_name": "Singh",
    "company": "",
    "address_1": "1257 ",
    "address_2": "Coach House Court",
    "city": "Fullerton",
    "state": "CA",
    "postcode": "92831",
    "country": "US",
    "email": "daman@singhdd.com",
    "phone": "(122) 274-5555"
  },
  "shipping": {
    "first_name": "Damandeep",
    "last_name": "Singh",
    "company": "",
    "address_1": "1257",
    "address_2": "Coach House Court",
    "city": "Fullerton",
    "state": "CA",
    "postcode": "92831",
    "country": "US"
  },
  "payment_method": "paypal_express",
  "payment_method_title": "Paypal Express",
  "transaction_id": "",
  "customer_ip_address": "103.41.36.35",
  "customer_user_agent": "PostmanRuntime/3.0.9",
  "created_via": "rest-api",
  "customer_note": "",
  "date_completed": "2017-02-02T08:41:46",
  "date_paid": "",
  "cart_hash": "",
  "line_items": [
    {
      "id": 79,
      "name": "Kaju Katli",
      "sku": "SW-282",
      "product_id": 491,
      "variation_id": 494,
      "quantity": 1,
      "tax_class": "",
      "price": "15.98",
      "subtotal": "15.98",
      "subtotal_tax": "1.44",
      "total": "15.98",
      "total_tax": "1.44",
      "taxes": [
        {
          "id": 1,
          "total": 1.4382,
          "subtotal": 1.4382
        }
      ],
      "meta": [
        {
          "key": "packing-size",
          "label": "Packing Size",
          "value": "2lb Box"
        }
      ]
    }
  ],
  "tax_lines": [
    {
      "id": 81,
      "rate_code": "SALES TAX-1",
      "rate_id": "1",
      "label": "Sales Tax",
      "compound": false,
      "tax_total": "1.44",
      "shipping_tax_total": "1.22"
    }
  ],
  "shipping_lines": [
    {
      "id": 80,
      "method_title": "USPS Medium Flat Rate Box",
      "method_id": "usps_medium_box",
      "total": "13.60",
      "total_tax": "0.00",
      "taxes": []
    }
  ],
  "fee_lines": [],
  "coupon_lines": [],
  "refunds": [],
  "_links": {
    "self": [
      {
        "href": "https://ambala.webdemos.cf/wp-json/wc/v1/orders/605"
      }
    ],
    "collection": [
      {
        "href": "https://ambala.webdemos.cf/wp-json/wc/v1/orders"
      }
    ],
    "customer": [
      {
        "href": "https://ambala.webdemos.cf/wp-json/wc/v1/customers/2"
      }
    ]
  }
}




JSON输出应该是这样的。



{
  "name": "Gijs Boersma",
  "street": "Lange laan",
  "house_number": "29",
  "house_number_extension": "a",
  "zipcode": "9281EM",
  "city": "Zevenaar",
  "telephone": "0602938172",
  "email": "noreply@example.com",
  "reference": "Bestelling 112",
  "pick_up_point": {
    "uuid": "560db083-d941-425b-b3b6-b813718297e1"
  },
  "product": "sameday_parcel_medium",
  "product_options": [
    {
      "option": "allow_neighbours",
      "value": false
    },
    {
      "option": "require_signature",
      "value": false
    },
    {
      "option": "age_check_18",
      "value": false
    },
    {
      "option": "perishable",
      "value": true,
      "max_attempts": 2
    }
  ]
}




1 个答案:

答案 0 :(得分:0)

您可以通过订单过滤器执行此操作。

add_filter( 'woocommerce_rest_prepare_shop_order_object', 'change_shop_order_response', 10, 3 );
function change_shop_order_response( $response, $item, $request ) {

//Do your stuff here with the $response object.

return $response
}