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

时间:2018-03-18 15:03:11

标签: android woocommerce woocommerce-rest-api

我正在尝试通过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": [
              "test@gnail.com"
            ]
          }
        }
      ]
    }
  ],
  "refunds": [],
  "_links": {
    "self": [
      {
        "href": "https://.../wp-json/wc/v2/orders/23136"
      }
    ],
    "collection": [
      {
        "href": "https://.../wp-json/wc/v2/orders"
      }
    ]
  }
}

任何人都知道为什么会这样? 提前谢谢

1 个答案:

答案 0 :(得分:0)

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": [
              "test@gnail.com"
            ]
          }
        }
      ]
    }
  ],
  "refunds": [],
  "_links": {
    "self": [
      {
        "href": "https://.../wp-json/wc/v2/orders/23136"
      }
    ],
    "collection": [
      {
        "href": "https://.../wp-json/wc/v2/orders"
      }
    ]
  }
}