It seems we can’t find what you’re looking for. Perhaps searching can help.
/** * Affiliate Welcome Coupon for WooCommerce + SliceWP. * Creates a unique 15% coupon for a newly registered customer * referred by a valid SliceWP affiliate. * * Does not modify affiliate commissions or store credit. */ defined( 'ABSPATH' ) || exit; add_action( 'woocommerce_created_customer', 'awc_create_referred_customer_coupon', 20, 3 ); function awc_create_referred_customer_coupon( $customer_id, $new_customer_data = array(), $password_generated = false ) { if ( ! function_exists( 'slicewp_get_affiliate' ) || ! class_exists( 'WC_Coupon' ) || ! function_exists( 'wc_get_coupon_id_by_code' ) ) { return; } $customer_id = absint( $customer_id ); if ( ! $customer_id ) { return; } // Prevent duplicate coupon generation. if ( get_user_meta( $customer_id, '_awc_welcome_coupon', true ) ) { return; } $customer = get_userdata( $customer_id ); if ( ! $customer || ! is_email( $customer->user_email ) ) { return; } // SliceWP stores the referring affiliate ID in this cookie. $affiliate_id = isset( $_COOKIE['slicewp_aff'] ) ? absint( wp_unslash( $_COOKIE['slicewp_aff'] ) ) : 0; if ( ! $affiliate_id ) { return; } // Validate the affiliate against SliceWP's database. $affiliate = slicewp_get_affiliate( $affiliate_id ); if ( ! $affiliate || ! method_exists( $affiliate, 'get' ) ) { return; } if ( 'active' !== $affiliate->get( 'status' ) ) { return; } // Do not issue a customer welcome coupon to the affiliate themselves. if ( function_exists( 'slicewp_get_affiliate_by_user_id' ) && slicewp_get_affiliate_by_user_id( $customer_id ) ) { return; } // Create a unique, unpredictable coupon code. $coupon_code = ''; for ( $attempt = 0; $attempt < 5; $attempt++ ) { $candidate = 'WELCOME15-' . strtoupper( wp_generate_password( 16, false, false ) ); if ( ! wc_get_coupon_id_by_code( $candidate ) ) { $coupon_code = $candidate; break; } } if ( ! $coupon_code ) { return; } try { $coupon = new WC_Coupon(); $coupon->set_code( $coupon_code ); $coupon->set_discount_type( 'percent' ); $coupon->set_amount( 15 ); // Only one successful use in total. $coupon->set_usage_limit( 1 ); // One use per customer as an additional safeguard. $coupon->set_usage_limit_per_user( 1 ); // Restrict it to the referred customer's email. $coupon->set_email_restrictions( array( sanitize_email( $customer->user_email ) ) ); $coupon->set_individual_use( true ); $coupon->set_description( '15% welcome coupon issued to a SliceWP-referred customer.' ); $coupon_id = $coupon->save(); if ( ! $coupon_id ) { return; } // Save the code against the customer only after coupon creation. update_user_meta( $customer_id, '_awc_welcome_coupon', $coupon_code ); update_user_meta( $customer_id, '_awc_referring_affiliate', $affiliate_id ); } catch ( Throwable $e ) { error_log( 'Affiliate Welcome Coupon: ' . $e->getMessage() ); } } /** * Display the coupon on WooCommerce My Account > Dashboard. */ add_action( 'woocommerce_account_dashboard', 'awc_display_referred_customer_coupon' ); function awc_display_referred_customer_coupon() { if ( ! is_user_logged_in() || ! function_exists( 'wc_get_coupon_id_by_code' ) ) { return; } $customer_id = get_current_user_id(); $coupon_code = get_user_meta( $customer_id, '_awc_welcome_coupon', true ); if ( ! $coupon_code ) { return; } $coupon_id = wc_get_coupon_id_by_code( $coupon_code ); if ( ! $coupon_id ) { return; } $coupon = new WC_Coupon( $coupon_id ); if ( ! $coupon->get_id() || $coupon->get_usage_count() >= 1 || ! $coupon->is_valid() ) { return; } $customer = get_userdata( $customer_id ); if ( ! $customer ) { return; } $allowed_emails = array_map( 'strtolower', $coupon->get_email_restrictions() ); if ( ! in_array( strtolower( $customer->user_email ), $allowed_emails, true ) ) { return; } echo '
' . esc_html__( 'Use your personal coupon at checkout:', 'woocommerce' ) . '
'; echo '' .
esc_html( $coupon_code ) .
'
' . esc_html__( 'This coupon is restricted to your email address and can be used once.', 'woocommerce' ) . '
'; echo 'It seems we can’t find what you’re looking for. Perhaps searching can help.
Sign up for updates on new compounds, research resources, and product availability — and get 20% off your first order.