I am looking for a documentation regarding setting a coupon’s used_by particular customer user_id on Woocommerce if coupon code to be added manually via REST API or from the Admin create new order endpoint. But I find no way to do it correctly. Hoping someone could point me out. Below is my code developed so far but it returned NULL.
$WC_Coupon = new WC_Coupon($request['code']);
$WC_Coupon->set_used_by( $request['customer_id'] );
2
Answers
Found the answer...The right function to use is increase_usage_count instead of directly using the Coupon object to set the used_by meta key.
Hope this snippet helps whoever that creates order via REST API and in need of a method to track who has consumed the coupon code.
Are you sure that your
$WC_Coupon
is instantiated properly? No matter how a coupon was added into the system, once it is there, it should act like any other coupon.Would suggest you to take a var_dump of $WC_Coupon and see if it outputs a proper value otherwise, you might not be providing the constructor with a proper code parameter.