Steps to Reproduce:
- Create a new coupon (or use the id of an existing one).
- Delete it with an http request to
DELETE /wp-json/wc/v3/coupons/<id>as per the WooCommerce Rest Api Documentation. The resulting response always seems to be
{
"code": "woocommerce_rest_cannot_delete",
"message": "The shop_coupon cannot be deleted.",
"data": {
"status": 500
}
}
Even though the checking the coupons status, which is now trash, shows that the deletion was successful.
Did not find any record of this problem posted online. I tracked the problem to the Woo source code.
class-wc-rest-crud-controller.php
$object->delete();
$result = 'trash' === $object->get_status();
...
if ( ! $result ) {
/* translators: %s: post type */
return new WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 500 ) );
}
The above code looks ok, it’s reasonable that the status of the object should change to trash upon a successful call to $object->delete(); . However, looking closer at the code in WC_Coupon delete()
wp_trash_post( $id );
do_action( 'woocommerce_trash_coupon', $id );
The trashing of the coupon is accomplished with a global function which is independent of the WC_Coupon object that is saved in $object. Therefore the line from above $result = 'trash' === $object->get_status(); will always come back false.