Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce Order API always creates empty orders

Im playing around with Woo Commerce API (v3.4.4). I can get a product with:

curl -X GET \
  'http://localhost/wp-json/wc/v2/products/7?oauth_consumer_key=ck_8cbe42fb09c04954a63994c22145270f27871dec&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1535381325&oauth_nonce=iApWJblA1A5&oauth_version=1.0&oauth_signature=9T1T43FvOpBzVBU+RQS90zMB/XM=' \
  -H 'Cache-Control: no-cache' \
  -H 'Postman-Token: bdb7f5c9-5719-4bcf-b4fc-38aed3bd5d08'

I get the response:

{
    "id": 7,
    "name": "Test product",
    "slug": "test-product",
    "permalink": "http://localhost/product/test-product/",
    "date_created": "2018-08-22T15:25:26",
    "date_created_gmt": "2018-08-22T15:25:26",
    "date_modified": "2018-08-22T15:25:26",
    "date_modified_gmt": "2018-08-22T15:25:26",
    "type": "simple",
    "status": "publish",
    "featured": false,
    "catalog_visibility": "visible",
    "description": "<p>this is a test product</p>\n",
    "short_description": "<p>the short desc</p>\n",
    "sku": "",
    "price": "9",
    "regular_price": "10",
    "sale_price": "9",
    "date_on_sale_from": null,
    "date_on_sale_from_gmt": null,
    "date_on_sale_to": null,
    "date_on_sale_to_gmt": null,
    "price_html": "<del><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&euro;</span>10.00</span></del> <ins><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&euro;</span>9.00</span></ins>",
    "on_sale": true,
    "purchasable": true,
    "total_sales": 3,
    "virtual": false,
    "downloadable": false,
    "downloads": [],
    "download_limit": -1,
    "download_expiry": -1,
    "external_url": "",
    "button_text": "",
    "tax_status": "taxable",
    "tax_class": "",
    "manage_stock": false,
    "stock_quantity": null,
    "in_stock": true,
    "backorders": "no",
    "backorders_allowed": false,
    "backordered": false,
    "sold_individually": false,
    "weight": "",
    "dimensions": {
        "length": "",
        "width": "",
        "height": ""
    },
    "shipping_required": true,
    "shipping_taxable": true,
    "shipping_class": "",
    "shipping_class_id": 0,
    "reviews_allowed": true,
    "average_rating": "0.00",
    "rating_count": 0,
    "related_ids": [
        36,
        41
    ],
    "upsell_ids": [],
    "cross_sell_ids": [],
    "parent_id": 0,
    "purchase_note": "",
    "categories": [
        {
            "id": 15,
            "name": "Uncategorized",
            "slug": "uncategorized"
        }
    ],
    "tags": [],
    "images": [
        {
            "id": 8,
            "date_created": "2018-08-22T15:24:00",
            "date_created_gmt": "2018-08-22T15:24:00",
            "date_modified": "2018-08-22T15:24:00",
            "date_modified_gmt": "2018-08-22T15:24:00",
            "src": "http://localhost/wp-content/uploads/2018/08/sock.jpeg",
            "name": "sock",
            "alt": "",
            "position": 0
        }
    ],
    "attributes": [
        {
            "id": 0,
            "name": "size",
            "position": 0,
            "visible": true,
            "variation": false,
            "options": [
                "small",
                "medium",
                "large"
            ]
        }
    ],
    "default_attributes": [],
    "variations": [],
    "grouped_products": [],
    "menu_order": 0,
    "meta_data": [],
    "_links": {
        "self": [
            {
                "href": "http://localhost/wp-json/wc/v2/products/7"
            }
        ],
        "collection": [
            {
                "href": "http://localhost/wp-json/wc/v2/products"
            }
        ]
    }
}

This works running the request using CURL on the command line, and running the request in Postman.

But now I try to create an order:

curl -X POST \
  'http://localhost/wp-json/wc/v2/orders?oauth_consumer_key=ck_8cbe42fb09c04954a63994c22145270f27871dec&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1535382336&oauth_nonce=P8RWaD9DnZ9&oauth_version=1.0&oauth_signature=GFxtI7+gqbah5fu68gSdqL4V3fs=' \
  -H 'Cache-Control: no-cache' \
  -H 'Postman-Token: f17bec6a-c76b-4a6c-8797-1df9dac73f36' \
  -d '{
  "payment_method": "bacs",
  "payment_method_title": "Direct Bank Transfer",
  "set_paid": true,
  "billing": {
    "first_name": "John",
    "last_name": "Doe",
    "address_1": "969 Market",
    "address_2": "",
    "city": "San Francisco",
    "state": "CA",
    "postcode": "94103",
    "country": "US",
    "email": "[email protected]",
    "phone": "(555) 555-5555"
  },
  "shipping": {
    "first_name": "John",
    "last_name": "Doe",
    "address_1": "969 Market",
    "address_2": "",
    "city": "San Francisco",
    "state": "CA",
    "postcode": "94103",
    "country": "US"
  },
  "line_items": [
    {
      "product_id": 7,
      "quantity": 1
    }
  ],
  "shipping_lines": [
    {
      "method_id": "flat_rate",
      "method_title": "Flat Rate",
      "total": 7
    }
  ]
}'

When I run the above request with Postman, it creates an empty order and I get the response:

{
    "id": 59,
    "parent_id": 0,
    "number": "59",
    "order_key": "wc_order_5b8413f8ed585",
    "created_via": "rest-api",
    "version": "3.4.4",
    "status": "pending",
    "currency": "EUR",
    "date_created": "2018-08-27T15:08:40",
    "date_created_gmt": "2018-08-27T15:08:40",
    "date_modified": "2018-08-27T15:08:41",
    "date_modified_gmt": "2018-08-27T15:08:41",
    "discount_total": "0.00",
    "discount_tax": "0.00",
    "shipping_total": "0.00",
    "shipping_tax": "0.00",
    "cart_tax": "0.00",
    "total": "0.00",
    "total_tax": "0.00",
    "prices_include_tax": false,
    "customer_id": 0,
    "customer_ip_address": "",
    "customer_user_agent": "",
    "customer_note": "",
    "billing": {
        "first_name": "",
        "last_name": "",
        "company": "",
        "address_1": "",
        "address_2": "",
        "city": "",
        "state": "",
        "postcode": "",
        "country": "",
        "email": "",
        "phone": ""
    },
    "shipping": {
        "first_name": "",
        "last_name": "",
        "company": "",
        "address_1": "",
        "address_2": "",
        "city": "",
        "state": "",
        "postcode": "",
        "country": ""
    },
    "payment_method": "",
    "payment_method_title": "",
    "transaction_id": "",
    "date_paid": null,
    "date_paid_gmt": null,
    "date_completed": null,
    "date_completed_gmt": null,
    "cart_hash": "",
    "meta_data": [],
    "line_items": [],
    "tax_lines": [],
    "shipping_lines": [],
    "fee_lines": [],
    "coupon_lines": [],
    "refunds": [],
    "_links": {
        "self": [
            {
                "href": "http://localhost/wp-json/wc/v2/orders/59"
            }
        ],
        "collection": [
            {
                "href": "http://localhost/wp-json/wc/v2/orders"
            }
        ]
    }
}

Bizarrely, when i run the EXACT same request with CURL on the command line, I get the response:

{"code":"woocommerce_rest_authentication_error","message":"Invalid signature - provided signature does not match.","data":{"status":401}}

If I run the request in Postman, but for "product_id" I use "this is a completely fake id" it still creates an empty order and responds with:

{
    "id": 60,
    "parent_id": 0,
    "number": "60",
    "order_key": "wc_order_5b84148e002da",
    "created_via": "rest-api",
    "version": "3.4.4",
    "status": "pending",
    "currency": "EUR",
    "date_created": "2018-08-27T15:11:10",
    "date_created_gmt": "2018-08-27T15:11:10",
    "date_modified": "2018-08-27T15:11:10",
    "date_modified_gmt": "2018-08-27T15:11:10",
    "discount_total": "0.00",
    "discount_tax": "0.00",
    "shipping_total": "0.00",
    "shipping_tax": "0.00",
    "cart_tax": "0.00",
    "total": "0.00",
    "total_tax": "0.00",
    "prices_include_tax": false,
    "customer_id": 0,
    "customer_ip_address": "",
    "customer_user_agent": "",
    "customer_note": "",
    "billing": {
        "first_name": "",
        "last_name": "",
        "company": "",
        "address_1": "",
        "address_2": "",
        "city": "",
        "state": "",
        "postcode": "",
        "country": "",
        "email": "",
        "phone": ""
    },
    "shipping": {
        "first_name": "",
        "last_name": "",
        "company": "",
        "address_1": "",
        "address_2": "",
        "city": "",
        "state": "",
        "postcode": "",
        "country": ""
    },
    "payment_method": "",
    "payment_method_title": "",
    "transaction_id": "",
    "date_paid": null,
    "date_paid_gmt": null,
    "date_completed": null,
    "date_completed_gmt": null,
    "cart_hash": "",
    "meta_data": [],
    "line_items": [],
    "tax_lines": [],
    "shipping_lines": [],
    "fee_lines": [],
    "coupon_lines": [],
    "refunds": [],
    "_links": {
        "self": [
            {
                "href": "http://localhost/wp-json/wc/v2/orders/60"
            }
        ],
        "collection": [
            {
                "href": "http://localhost/wp-json/wc/v2/orders"
            }
        ]
    }
}

So I have a number of problems:

1/ No matter what product_id I use, the order that's created is always empty

2/ Even if I provide a fake product_id, an order is still created by Woo Commerce. Surely this is incorrect?

3/ Running the create order requests on the command line is giving different responses to Postman

like image 738
Mark Avatar asked Aug 27 '18 15:08

Mark


People also ask

How does WooCommerce order status work?

Order statuses let you know how far along the order is, starting with “Pending payment” and ending with “Completed.” The following order statuses are used: Pending payment — Order received, no payment initiated. Awaiting payment (unpaid).

How do I change my order status in WooCommerce?

Go to WooCommerce > Settings > Order Statuses, and set up new statuses or email triggers. Edit your email content if needed. That's it! Sit back and enjoy custom WooCommerce order statuses and automatic emails!

What does processing mean WooCommerce?

Processing: This means the customer's payment has been received. The order is now awaiting fulfilment, i.e. for you to process it. If you're using WooCommerce to track your orders, you can click into the order and change the order status to “Completed” once you've dispatched the product(s).


1 Answers

I was missing:

-H 'Content-Type: application/json' \

from the request. I had this entered in Postman, but had not ticked the check box:

enter image description here

like image 99
Mark Avatar answered Sep 20 '22 22:09

Mark