'Add restriction to WooCommerce coupons by allowed user ID
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?
Solution 1:[1]
To keep it dynamic, you can use the following code to add a new field to the usage restriction tab:
// Add new field - usage restriction tab
function action_woocommerce_coupon_options_usage_restriction( $coupon_get_id, $coupon ) {
woocommerce_wp_text_input( array(
'id' => 'customer_user_id',
'label' => __( 'User ID restrictions', 'woocommerce' ),
'placeholder' => __( 'No restrictions', 'woocommerce' ),
'description' => __( 'List of allowed user IDs. Separate user IDs with commas.', 'woocommerce' ),
'desc_tip' => true,
'type' => 'text',
));
}
add_action( 'woocommerce_coupon_options_usage_restriction', 'action_woocommerce_coupon_options_usage_restriction', 10, 2 );
// Save
function action_woocommerce_coupon_options_save( $post_id, $coupon ) {
// Isset
if ( isset ( $_POST['customer_user_id'] ) ) {
$coupon->update_meta_data( 'customer_user_id', sanitize_text_field( $_POST['customer_user_id'] ) );
$coupon->save();
}
}
add_action( 'woocommerce_coupon_options_save', 'action_woocommerce_coupon_options_save', 10, 2 );
Where you can enter the userID(s), separated by commas. (see attached image)
Along with this code for the coupon validation:
// Valid
function filter_woocommerce_coupon_is_valid( $valid, $coupon, $discount ) {
// Get meta
$customer_user_id = $coupon->get_meta( 'customer_user_id' );
// NOT empty
if ( ! empty( $customer_user_id ) ) {
// Convert string to array
$customer_user_id = explode( ', ', $customer_user_id );
// Get current user id
$user_id = get_current_user_id();
if ( ! in_array( $user_id, $customer_user_id ) ) {
$valid = false;
if ( ! $valid ) {
throw new Exception( __( 'My error message', 'woocommerce' ), 109 );
}
}
}
return $valid;
}
add_filter( 'woocommerce_coupon_is_valid', 'filter_woocommerce_coupon_is_valid', 10, 3 );
Solution 2:[2]
There is no direct way of restricting coupons by user ID. But there is a workaround by adding user ID with coupon metadata.
$user_id = get_current_user_id();
update_post_meta( $new_coupon_id, 'couponuserid', $user_id );
and checking hen applying the coupon.
add_filter( 'woocommerce_coupon_is_valid', function( $is_valid, $coupon ) {
/**
* Selected coupons allowed only for selected User IDs
*/
$is_userid_coupon = get_post_meta( $coupon->get_id(), 'couponuserid', true );
if ( isset( $is_userid_coupon ) ) {
$user = wp_get_current_user();
if ( $user->ID && $user->ID == get_post_meta( $coupon->get_id(), 'couponuserid', true ) ) {
return true;
}
}
return $is_valid;
}, 100, 2 );
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | mujuonly |