json - 自定义 JSON 格式 webhook WooCommerce Wordpress

标签 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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d494c404c436d5e44434a454949034e4240" rel="noreferrer noopener nofollow">[email protected]</a>",
    "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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7816170a1d081401381d00191508141d561b1715" rel="noreferrer noopener nofollow">[email protected]</a>",
  "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
    }
  ]
}

最佳答案

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

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
}

关于json - 自定义 JSON 格式 webhook WooCommerce Wordpress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49223236/

相关文章:

json - 配置单元是否允许列名为 "rows"?

javascript - 将 JSON 添加到选项值

php - 如何通过 Adob​​e Muse 生成的 CSS 运行 PHP

php - 在 WooCommerce 中显示含增值税和不含增值税的单一产品价格

php - WP - woocommerce 按订单列出产品类别

php - WooCommerce:管理员中的动态自定义结帐字段

javascript - Jquery 提交处理程序有两个按钮?

android - JSON 解析的问题

widget - Wordpress 小部件如何仅根据选定的父类别显示子类别?

wordpress - 如何使用 docker 自动化 wp 实例设置 + 主题/插件定制?