Search code examples
androidwoocommercewoocommerce-rest-api

Create an order with Coupon_lines in woocomerce rest api


I am trying to create an order with coupon in an android app through woocommerce rest api. I can get coupon information, also I can create the order, but discount does not apply. order creates with it's original price!

without coupons every thing works fine.

here is the order response from API (I removed some useless data from it)

{
  "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"
      }
    ]
  }
}

any one has any idea why it act like this? thanks in advance


Solution

  • WooCommerce Orders create not compatible for counting the discount and deduct from the total amount of the order. https://github.com/woocommerce/woocommerce/issues/11358

    So you have to do it in your own way. I have a solution to customize the create order API for the counting the discount.

    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;
    }
    

    Now Place your order as below request.

    {
      "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"
          }
        ]
      }
    }