【问题标题】:Custom JSON format webhook WooCommerce Wordpress自定义 JSON 格式 webhook WooCommerce Wordpress
【发布时间】:2018-03-15 21:11:36
【问题描述】:

我希望我的 Woocommerce 商店连接到一家运输包裹公司。没有现有的 WooCommerce 插件。我想用 webhook 来做,在 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
    }
  ]
}

【问题讨论】:

    标签: json wordpress woocommerce webhooks


    【解决方案1】:

    您可以通过订单过滤器来做到这一点。

    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
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-25
      • 2016-01-07
      • 2017-05-24
      相关资源
      最近更新 更多