WooCommerce:手动验证优惠券用户电子邮件

时间:2019-09-12 10:30:22

标签: wordpress woocommerce hook-woocommerce

我有一个优惠券代码“ SIGNUP10”,该优惠券代码仅对在我们的mailchimp列表中具有其电子邮件地址的用户开放。我知道优惠券可以限制为“允许的电子邮件”下的某些电子邮件地址,但是我的mailchimp列表很大,我需要确保应用此优惠券的用户的电子邮件都在我们的列表中。

在输入优惠券之后,在验证优惠券并将其应用于购物车之前,是否有woocoomerce挂钩可以让我检查电子邮件地址?

1 个答案:

答案 0 :(得分:1)

您可以使用此钩子:

  

woocommerce_applied_coupon

add_action( 'woocommerce_applied_coupon', 'restrict_coupon_usage_with_my_email_list' );
function restrict_coupon_usage_with_my_email_list( $coupon_code )
{
    //create an array with your email list
    $emailArr = array("a@a.com", "b@b.com");
    $isSpecialCoupon = false;

    //check the $coupon_code 
    if($coupon_code == "a_b_coupon" && is_user_logged_in())
    {
       //if it is the coupon code you want to restrict with those emails and if user logged in
       $isSpecialCoupon = true;
    }
    if($isSpecialCoupon)
    {
       //check if the logged in user's email is in this array
       $current_user = wp_get_current_user();
       if(in_array($current_user->user_email, $emailArr)) return true;
       else WC()->cart->remove_coupon( $coupon_code );
    }
    else WC()->cart->remove_coupon( $coupon_code );
}

经过测试,工作正常(除了优惠券消息,该消息也可以编辑),但希望对您有所帮助。

顺便说一句:我相信使用常规的“允许的电子邮件”功能可以提高速度和性能。

其他:您也可以检查此免费插件以限制WooCommerce优惠券:https://wordpress.org/plugins/global-coupons-for-woocommerce/