woocommerce / wc-api-python

A Python wrapper for the WooCommerce API.
https://pypi.org/project/WooCommerce/
MIT License
213 stars 113 forks source link

Call API to create order, receive 201 status code but order does not show up correctly in dashboard #69

Closed artyom-boyarov closed 3 years ago

artyom-boyarov commented 3 years ago

Hi all,

I am creating some code which will receive orders from a database, parse the order object into JSON and then create an order on a Woocommerce site using the Woocommerce python API. I get a response of 201, along with some links, however the order is not created correctly. I can see in the dashboard on Wordpress of the Woocommerce app that a new order has been added, along with an ID for it, however the shipping information, billing information or total cost has not been added, despite my JSON containing those fields. If I click on some of the links, I JSON for the newly-created order - again, without any shipping information or billing information.

I would be extremely grateful if any of you could point me to a solution for this problem. Here is my code:

def process_order(db_order: TBLOrder):
    result_dict = dict()
    db_order.meta_data.replace("\"", "")
    order_payload = str({
        "id": db_order.order_id,
        "parent_id": db_order.parent_id,
        "number": db_order.number,
        "order_key": db_order.order_key,
        "created_via": db_order.created_via,
        "status": db_order.status,
        "currency": db_order.currency,
        "date_created": str(db_order.date_created),
        "date_created_gmt": str(db_order.date_created_gmt),
        "date_modified": str(db_order.date_modified),
        "date_modified_gmt": str(db_order.date_modified_gmt),
        "discount_total": db_order.discount_total,
        "discount_tax": db_order.discount_tax,
        "shipping_total": db_order.shipping_total,
        "shipping_tax": db_order.shipping_tax,
        "cart_tax": db_order.cart_tax,
        "total": db_order.total,
        "total_tax": db_order.total_tax,
        "prices_include_tax": db_order.prices_include_tax,
        "customer_id": db_order.customer_id,
        "customer_ip_address": db_order.customer_ip_address,
        "customer_user_agent": db_order.customer_user_agent,
        "customer_note": db_order.customer_note,
        "billing": {
            "first_name": db_order.billing_first_name,
            "last_name": db_order.billing_last_name,
            "address_1": db_order.billing_address1,
            "address_2": db_order.billing_address2,
            "city": db_order.billing_city,
            "state": db_order.billing_state,
            "postcode": db_order.billing_postcode,
            "country": db_order.billing_country,
            "email": db_order.billing_email,
            "phone": db_order.billing_phone
        },
        "shipping": {
            "first_name": db_order.shipping_first_name,
            "last_name": db_order.shipping_last_name,
            "address_1": db_order.shipping_address1,
            "address_2": db_order.shipping_address2,
            "city": db_order.shipping_city,
            "state": db_order.shipping_state,
            "postcode": db_order.shipping_postcode,
            "country": db_order.shipping_country
        },
        "payment_method": db_order.payment_method,
        "payment_method_title": db_order.payment_method_title,
        "transaction_id": db_order.transaction_id,
        "date_paid": str(db_order.date_paid),
        "date_paid_gmt": str(db_order.date_paid_gmt),
        "date_completed": str(db_order.date_completed),
        "date_completed_gmt": str(db_order.date_completed_gmt),
        "cart_hash": db_order.cart_hash,
        "meta_data": json.loads(db_order.meta_data),
        "line_items": json.loads(db_order.line_items),
        "shipping_lines": json.loads(db_order.shipping_lines),
        "fee_lines": json.loads(db_order.fee_lines),
        "tax_lines": json.loads(db_order.tax_lines),
        "coupon_lines": json.loads(db_order.coupon_lines),
        "refunds": json.loads(db_order.refunds)
    })
    order_payload = order_payload.replace("'", '"')
    print(order_payload)
    wcapi = API(
        url=CMS_STORE_URL,
        consumer_key=CMS_STORE_KEY,
        consumer_secret=CMS_STORE_SECRET,
        version="wc/v3"
    )
    order_response = wcapi.post("orders", order_payload)
    print(order_response.content)
    if order_response.status_code == 200 or order_response.status_code == 201:
        result_dict[RESULT_CODE_SUCCESSFUL] = f'Added order {db_order.number}'
    else:
        result_dict[RESULT_CODE_ERROR] = f'Error adding order {db_order.number} with message: ' \
                                         f'{order_response.json()["message"]}'
    return result_dict

The response:

{
  "currency_symbol": "\\u00a3",
  "_links": {
    "self": [
      {
        "href": "https:\\/\\/www.XXX.co.uk\\/wp-json\\/wc\\/v3\\/orders\\/381"
      }
    ],
    "collection": [
      {
        "href": "https:\\/\\/www.XXX.co.uk\\/wp-json\\/wc\\/v3\\/orders"
      }
    ]
  }
}

What I see from the links:

{
  "id": 1,
  "parent_id": 0,
  "status": "pending",
  "currency": "XYZ",
  "version": "5.3.0",
  "prices_include_tax": false,
  "date_created": "2021-06-14T12:36:01",
  "date_modified": "2021-06-14T12:36:01",
  "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",
  "customer_id": 0,
  "order_key": "XXX",
  "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": "",
  "customer_ip_address": "",
  "customer_user_agent": "",
  "created_via": "rest-api",
  "customer_note": "",
  "date_completed": null,
  "date_paid": null,
  "cart_hash": "",
  "number": "1",
  "meta_data": [],
  "line_items": [],
  "tax_lines": [],
  "shipping_lines": [],
  "fee_lines": [],
  "coupon_lines": [],
  "refunds": [],
  "date_created_gmt": "2021-06-14T12:36:01",
  "date_modified_gmt": "2021-06-14T12:36:01",
  "date_completed_gmt": null,
  "date_paid_gmt": null,
  "currency_symbol": "\\u00a3",
  "_links": {
    "self": [
      {
        "href": "https:\\/\\/www.XXX.co.uk\\/wp-json\\/wc\\/v3\\/orders\\/377"
      }
    ],
    "collection": [
      {
        "href": "https:\\/\\/www.XXX.co.uk\\/wp-json\\/wc\\/v3\\/orders"
      }
    ]
  }
}

Any help will be greatly appreciated.

Thank you in advance,

Heemogoblin