Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating coupon through API and ruby returns error: woocommerce_api_missing_coupon_data

I am trying to create a coupon though the rest api from my rails 4 app using the sample code from the documentation on this page: https://woocommerce.github.io/woocommerce-rest-api-docs/?ruby#create-a-coupon

Here is the code I am using:

 data = {
      code: "10off",
      discount_type: "percent",
      amount: "10",
      individual_use: true,
      exclude_sale_items: true,
      minimum_amount: "100.00"
    }
woocommerce.post("coupons", data).parsed_response

I expect it to return a successful response from the API instead I get {"errors"=>[{"code"=>"woocommerce_api_missing_coupon_data", "message"=>"No coupon data specified to create coupon"}]}

like image 310
Aaron Mills Avatar asked Jul 17 '19 13:07

Aaron Mills


1 Answers

The paragraph below shows how it was working, at least in API V2 and V3.

The params / data hash contained a coupon attribute, see v3 documentation:

data = {
  coupon: {
    code: "new-coupon"
    ...
  }
}

After some research, I am still not sure that the example in the API description is right, as you can see here.

The code below is available for V2 and V3 and there is no other code available, V3 was updated 13 days ago. The error is raised when 'coupon' is not available in data:

if ( ! isset( $data['coupon'] ) ) {
 throw new WC_API_Exception( 'woocommerce_api_missing_coupon_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'coupon' ), 400 );
}

I suggest trying with this code:

data = {
  coupon: {
      code: "10off",
      discount_type: "percent",
      amount: "10",
      individual_use: true,
      exclude_sale_items: true,
      minimum_amount: "100.00"
  }
}
woocommerce.post("coupons", data).parsed_response

If this works I would stick to the V3 documentation for the time being or raise an issue e.g. by creating a ticket with Woocommerce. Maybe their documentation team worked faster than development :)

like image 54
Christian Avatar answered Oct 12 '22 23:10

Christian