首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在woocomerce中使用Coupon_lines创建订单

在woocomerce中使用Coupon_lines创建订单
EN

Stack Overflow用户
提问于 2018-03-18 15:03:10
回答 1查看 2.1K关注 0票数 2

我正试图通过woocommerce rest在android应用程序中创建一个带有优惠券的订单。我可以得到优惠券信息,也可以创建订单,但折扣不适用。订单创造了它的原价!

没有优惠券,一切都很好。

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

代码语言:javascript
复制
{
  "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"
      }
    ]
  }
}

有人知道为什么会这样吗?提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-19 09:11:06

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

所以你必须用你自己的方式去做。我有一个自定义create的解决方案,用于计算折扣。

代码语言:javascript
复制
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;
}

现在按以下要求下订单。

代码语言:javascript
复制
{
  "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"
      }
    ]
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49349396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档