Created
February 20, 2016 11:40
-
-
Save moghanbari/0984bad09d6287ef96a1 to your computer and use it in GitHub Desktop.
Restrict Coupon Code for a user. if the user is who you want, the coupon code will add to the cart.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'woocommerce_applied_coupon', 'check_for_individual_discount' ); | |
function check_for_individual_discount() | |
{ | |
global $woocommerce; | |
// Get All Coupons in Cart | |
$cart_coupons = $woocommerce->cart->applied_coupons; | |
$last_element = count($cart_coupons) -1; | |
$last_added_coupon = $cart_coupons[ $last_element ]; //get the last added coupon to the cart | |
$coupon_array = array(); | |
$coupon_array = explode('-', $last_added_coupon); | |
if( count($coupon_array) == 3 && $coupon_array[0] == 'fgccu') //if this coupon is for one user! | |
{ | |
//get user id | |
$userid = $coupon_array[2]; | |
$current_user = wp_get_current_user(); | |
if($current_user->ID != $userid) // if coupon user id is not equal with current user id remove the coupon! | |
{ | |
wc_clear_notices(); | |
wc_add_notice('This Code Can not use for Current User! 'error'); | |
$woocommerce->cart->remove_coupon( $last_added_coupon ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment