Let’s say i’m using this code to create a coupon:
$coupon_code = 'CODEOFF';
$amount = '10';
$discount_type = 'percent';
$coupon = array(
'post_title' => $coupon_code,
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);
$new_coupon_id = wp_insert_post( $coupon );
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'yes' );
update_post_meta( $new_coupon_id, 'product_ids', $some_id );
update_post_meta( $new_coupon_id, 'usage_limit', '1' );
update_post_meta( $new_coupon_id, 'expiry_date', $some_date );
Now I want to limit this code for only one user.
There is an option to use update_post_meta( $new_coupon_id, 'customer_email', '' );
, but my users don’t have emails.
How can I limit this coupon to a user using that user’s ID?
2
Answers
To keep it dynamic, you can use the following code to add a new field to the usage restriction tab:
Where you can enter the userID(s), separated by commas. (see attached image)
Along with this code for the coupon validation:
There is no direct way of restricting coupons by user ID. But there is a workaround by adding user ID with coupon metadata.
and checking hen applying the coupon.