在 woocomerce Rest api 中使用 Coupon_lines 创建订单

问题描述 投票:0回答:2

我正在尝试通过 woocommerce Rest api 在 Android 应用程序中创建带有优惠券的订单。我可以获取优惠券信息,也可以创建订单,但折扣不适用。订单以原价创建!

没有优惠券一切都很好。

这是API的订单响应(我从中删除了一些无用的数据)

{
  "id": 23136,
  "parent_id": 0,
  "number": "23136",
  "order_key": "wc_order_5aasdad3128608",
  "created_via": "rest-api",
  "version": "3.2.6",
  "status": "pending",
  "currency": "USD",
  "date_created": "2018-03-18T17:14:17",
  "date_created_gmt": "2018-03-18T13:44:17",
  "date_modified": "2018-03-18T17:14:17",
  "date_modified_gmt": "2018-03-18T13:44:17",
  "discount_total": "0",
  "discount_tax": "0",
  "shipping_total": "0",
  "shipping_tax": "0",
  "cart_tax": "0",
  "total": "67500",
  "total_tax": "0",
  "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": [
    {
      "id": 1792,
      "name": "...",
      "product_id": 22359,
      "variation_id": 22361,
      "quantity": 1,
      "tax_class": "",
      "subtotal": "67500",
      "subtotal_tax": "0",
      "total": "67500",
      "total_tax": "0",
      "taxes": [],
      "meta_data": [
        {
          "id": 19325,
          "key": "...",
          "value": "..."
        },
        {
          "id": 19326,
          "key": "...",
          "value": "..."
        }
      ],
      "sku": "",
      "price": 67500
    }
  ],
  "tax_lines": [],
  "shipping_lines": [],
  "fee_lines": [],
  "coupon_lines": [
    {
      "id": 1793,
      "code": "10Code",
      "discount": "10000.00",
      "discount_tax": "0",
      "meta_data": [
        {
          "id": 19329,
          "key": "coupon_data",
          "value": {
            "amount": "10000.00",
            "code": "10Code",
            "date_created": "2018-03-12T19:05:11",
            "date_created_gmt": "2018-03-12T15:35:11",
            "date_modified": "2018-03-12T19:15:35",
            "date_modified_gmt": "2018-03-12T15:45:35",
            "description": "",
            "discount_type": "fixed_cart",
            "email_restrictions": [],
            "exclude_sale_items": false,
            "excluded_product_categories": [],
            "excluded_product_ids": [],
            "free_shipping": false,
            "id": 20922,
            "individual_use": true,
            "limit_usage_to_x_items": 0,
            "maximum_amount": "0.00",
            "meta_data": [
              {
                "id": 469119,
                "key": "slide_template",
                "value": "default"
              }
            ],
            "minimum_amount": "0.00",
            "product_categories": [],
            "product_ids": [],
            "usage_count": 1,
            "usage_limit": 0,
            "usage_limit_per_user": 1,
            "used_by": [
              "[email protected]"
            ]
          }
        }
      ]
    }
  ],
  "refunds": [],
  "_links": {
    "self": [
      {
        "href": "https://.../wp-json/wc/v2/orders/23136"
      }
    ],
    "collection": [
      {
        "href": "https://.../wp-json/wc/v2/orders"
      }
    ]
  }
}

有人知道为什么会这样吗? 预先感谢

android woocommerce woocommerce-rest-api
2个回答
1
投票

WooCommerce 订单创建不兼容计算折扣并从订单总金额中扣除。 https://github.com/woocommerce/woocommerce/issues/11358

所以你必须以自己的方式去做。我有一个解决方案来自定义创建订单 API 来计算折扣。

add_filter( 'woocommerce_rest_prepare_shop_order_object', 'custom_change_shop_order_response', 10, 3 );
function custom_change_shop_order_response( $response, $object, $request ) {
 $order = wc_get_order( $response->data['id'] );
    $used_coupons = $request->get_param( 'coupon_lines' );
    $coupon_amount = 0;
    if( !empty( $used_coupons ) ):
        foreach ($used_coupons as $coupon ){
            $coupon_id = $coupon['id'];
            $coupon_amount = $coupon['amount'];
        }
    endif;

    $order_coupons = $reponse->data['coupon_lines'];
    if( !empty( $order_coupons ) ) :
        foreach ( $order_coupons as $coupon ) {
            wc_update_order_item_meta( $coupon['id'], 'discount_amount', $coupon['amount'] );
        }
    endif;
  $order_total = get_post_meta( $response->data['id'], '_order_total', true );
    $order_total = $order_total - $coupon_amount;
    update_post_meta( $order->ID, '_order_total', $order_total );
    $response->data['total']  = $order_total;

    return $response;
}

现在请按照以下要求下订单。

{
  "id": 23136,
  "parent_id": 0,
  "number": "23136",
  "order_key": "wc_order_5aasdad3128608",
  "created_via": "rest-api",
  "version": "3.2.6",
  "status": "pending",
  "currency": "USD",
  "date_created": "2018-03-18T17:14:17",
  "date_created_gmt": "2018-03-18T13:44:17",
  "date_modified": "2018-03-18T17:14:17",
  "date_modified_gmt": "2018-03-18T13:44:17",
  "discount_total": "0",
  "discount_tax": "0",
  "shipping_total": "0",
  "shipping_tax": "0",
  "cart_tax": "0",
  "total": "67500",
  "total_tax": "0",
  "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": [
    {
      "id": 1792,
      "name": "...",
      "product_id": 22359,
      "variation_id": 22361,
      "quantity": 1,
      "tax_class": "",
      "subtotal": "67500",
      "subtotal_tax": "0",
      "total": "67500",
      "total_tax": "0",
      "taxes": [],
      "meta_data": [
        {
          "id": 19325,
          "key": "...",
          "value": "..."
        },
        {
          "id": 19326,
          "key": "...",
          "value": "..."
        }
      ],
      "sku": "",
      "price": 67500
    }
  ],
  "tax_lines": [],
  "shipping_lines": [],
  "fee_lines": [],
  "coupon_lines": [
    {
      "id": 1793,
      "code": "10Code",
      "amount": "10000.00",
      "discount_tax": "0",
      "meta_data": [
        {
          "id": 19329,
          "key": "coupon_data",
          "value": {
            "amount": "10000.00",
            "code": "10Code",
            "date_created": "2018-03-12T19:05:11",
            "date_created_gmt": "2018-03-12T15:35:11",
            "date_modified": "2018-03-12T19:15:35",
            "date_modified_gmt": "2018-03-12T15:45:35",
            "description": "",
            "discount_type": "fixed_cart",
            "email_restrictions": [],
            "exclude_sale_items": false,
            "excluded_product_categories": [],
            "excluded_product_ids": [],
            "free_shipping": false,
            "id": 20922,
            "individual_use": true,
            "limit_usage_to_x_items": 0,
            "maximum_amount": "0.00",
            "meta_data": [
              {
                "id": 469119,
                "key": "slide_template",
                "value": "default"
              }
            ],
            "minimum_amount": "0.00",
            "product_categories": [],
            "product_ids": [],
            "usage_count": 1,
            "usage_limit": 0,
            "usage_limit_per_user": 1,
            "used_by": [
              "[email protected]"
            ]
          }
        }
      ]
    }
  ],
  "refunds": [],
  "_links": {
    "self": [
      {
        "href": "https://.../wp-json/wc/v2/orders/23136"
      }
    ],
    "collection": [
      {
        "href": "https://.../wp-json/wc/v2/orders"
      }
    ]
  }
}

0
投票

就我而言,优惠券代码显示在 woocommerce 订单详细信息页面中,但未应用,如图所示。

对我来说,从

wp-json/wc/v2/
更改为
wp-json/wc/v3/
后有效。我尝试为此进行手动工作,几天后,我发现解决方案是如此简单。

© www.soinside.com 2019 - 2024. All rights reserved.