Create order with PrestaShop's API

2019-05-31 03:28发布

I have a PrestaShop 1.6.0.9 and I'm trying to create a new order. I'm following the following steps:

  1. Create a cart with the security key of the client I have previously created, the id of the product I want to buy and the quantity.

  2. Then create a new order (sending a POST to /api/order), passing the client id, the security key, the product id, the prices (with and without taxes), etc... This is the entire XML that I send:

<?xml version="1.0" encoding="UTF-8"?><prestashop><order> <id_address_delivery>5</id_address_delivery> <id_address_invoice>5</id_address_invoice><id_currency>1</id_currency> <id_lang>1</id_lang><id_customer>2</id_customer><id_carrier>1</id_carrier> <current_state>2</current_state><valid>1</valid><module>cashondelivery</module> <payment>Pago en metalico</payment><conversion_rate>1</conversion_rate> <security_key>1e87b5d59d7c812a9d840a548e4b8a45</security_key><associations> <order_rows><order_rows><product_id>7</product_id> <product_attribute_id>34</product_attribute_id><product_quantity>1</product_quantity> </order_rows></order_rows></associations><id_cart>42</id_cart> <total_paid>24.85</total_paid><total_paid_real>24.85</total_paid_real> <total_products>20.54</total_products><total_products_wt>24.85</total_products_wt> </order></prestashop>

As you can see, I'm using the "cashondelivery" module. My problem is that when I post that XML to the API, the order get's created, but:

  1. Somehow the order doesn't show up in the orders list, but I can get to that order if I go to "Clients -> shopping carts -> "id of cart I created before POST-ing the order XML" -> order created from this cart.

  2. Once I'm in the order, I can't see the client details (because the id of the client doesn't get inserted). I'm pretty sure that the client with id 2 does exist, but I'm not sure why it isn't inserted with the POST I'm sending to the API.

Any idea why any of those two problems are happening?

2条回答
Explosion°爆炸
2楼-- · 2019-05-31 03:38

I'm posting the answer for anybody who gets here from Google. Even if PS docs say ids aren't required, you must pass every id field in both the cart and the order.

That means, you must provide a client id, an address id, delivery address id, etc... when you're creating the cart and when you're creating the order.

If you fail at doing so, yourorder might get inserted, or it might not. And, if it does get inserted, it's not guaranteed that it will show up in the "Orders" listing.

查看更多
太酷不给撩
3楼-- · 2019-05-31 03:45

There are some "hidden" mandatory things you need to follow in order to create a valid order:

  1. The secure_key must match between customer's secure_key and order's secure_key
  2. The address (of delivery) must be associated with a valid country.

So, the best way is that you follow the full flow of creating an order, naturally:

  1. Create customer, get customer's secure_key and customer_id from returned xml

  2. Create cart, using customer_id from above step

  3. Create order with customer_id, customer's secure_key and cart_id from above steps.

查看更多
登录 后发表回答