使用 WooCommerce Rest API 创建带有优惠券行的订单

时间:2021-01-28 10:28:29

标签: wordpress woocommerce woocommerce-rest-api coupon

我们正在尝试使用 woo commerce API 创建订单并应用优惠券代码,如 woo commerce 文档中所述。以下是我们尝试通过 API 请求发送的 JSON。

{
"order":{
"payment_details":{"method_id":"cash","method_title":"cash","paid":false},
"billing_address":{"first_name":"kamran","last_name":"masud","company":null,"address_1":"Dubai Dubai - United Arab Emirates",
    "address_2":null,"city":null,"state":null,"postcode":null,"country":null,"email":null,"phone":"541234566"},
"shipping_address":{"first_name":"kamran","last_name":"masud","company":null,"address_1":"Dubai Dubai - United Arab Emirates",
"address_2":null,"city":null,"state":null,"postcode":null,"country":null},
"customer_id":116,
"line_items":[
    {"product_id":11734,"variation_id":0,"quantity":108,"total":"3834"},
    {"product_id":10893,"variation_id":0,"quantity":1,"total":"206.56"},
    {"product_id":11872,"variation_id":0,"quantity":1,"total":"41.49"},
    {"product_id":11866,"variation_id":0,"quantity":1,"total":"53.17"},
    {"product_id":11865,"variation_id":0,"quantity":1,"total":"53.17"},
    {"product_id":11965,"variation_id":0,"quantity":1,"total":"47.81"},
    {"product_id":11626,"variation_id":0,"quantity":1,"total":"323.73"},
    {"product_id":11927,"variation_id":0,"quantity":1,"total":"144.3"}],
"shipping_lines":[],
"fee_lines":[
    {"id":0,"title":"Shipping :","tax_class":null,"total":"0","total_tax":null}
    ],
"coupon_lines":[
    {
    "code":"fixedcart10",
    "discount":"80",
    "meta_data":[
        {
            "key":"coupon_data",
            "value":[
            {
                "id":"168026",
                "code":"fixedcart10",
                "amount":"10"
            }
            ]
        }
    ]
    }
],
"discount_total":null,
"shipping_total":null,
"total":null,"status":"processing"}}

优惠券按照它未在创建的订单中应用的订单进行处理。以下是创建的订单的屏幕截图。 enter image description here

任何帮助将不胜感激。 谢谢

1 个答案:

答案 0 :(得分:0)

我认为,因为您手动传递了“总”值,所以它会覆盖 WooCommerce 通常应用于优惠券的自动计算。如果您只传递 product_id、variation_id 和数量,应该没问题。

但是,如果您要更新 WooCommerce 订单上的 line_items,则问题会更大一些,因为折扣是根据更新时传递的 sub_total(折扣前)和 total(折扣后)值计算得出的。