Issue:

Cần thêm data sản phẩm vào json output của Woocommerce Rest API để gửi thông tin đơn hàng cho CRM xử lý. Tuy nhiên Rest API trả về json output với sản phẩm chưa có một số thông tin như: hình ảnh, giá normal price…

{
      "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"
        }
      ]
    }

Solution:

Thêm product image, normal price vào order data thông qua hook woocommerce_rest_prepare_shop_order:

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
}

Kết quả:

Ref:

https://gist.github.com/bekarice/f1ba5b8e2f3c62d82a5cbc100dc3e1ad

https://stackoverflow.com/questions/49223236/custom-json-format-webhook-woocommerce-wordpress

https://stackoverflow.com/questions/48895252/woocommerce-rest-api-v2-issue-with-adding-custom-data-to-orders

Leave a Reply

Your email address will not be published. Required fields are marked *