WooCommerce Student Discount

时光总嘲笑我的痴心妄想 提交于 2019-12-08 03:30:19

问题


I need to create a coupon in WooCommerce that will only work if the user has an email which ends in .ac.uk.

For example student.name@uwl.ac.uk or teacher@kings.ac.uk.

The discount would be 10% off the total shopping basket.

I can't find any solutions online or any plugins which could help.

Any ideas?

Thanks.


回答1:


You can use this Auto apply Discount Coupon to Customer’s Purchase function with the right conditionals (user email finishing by ac.uk):

add_action('woocommerce_before_cart_table', 'coupon_auto_apply_user_email_tld', 10, 0);
function coupon_auto_apply_user_email_tld() {

    $current_user = wp_get_current_user();
    $user_email = $current_user->user_email; 
    $mail_part = explode('@', $user_email);
    $domain_part = explode('.', $mailParts[1]);
    $mail_tld = $domain_part[1] . '.' . $domain_part[2]);

    if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;

    if ( $mail_tld == 'ac.uk' ) {
        $coupon_code = 'UNIQUECODE'; // <= set the name of your coupon code here
        if ( ! WC()->cart->add_discount( sanitize_text_field( $coupon_code ) ) ) {
            WC()->show_messages();
        }
        echo '<div class="woocommerce_message"><strong>The total price in your cart will be discounted 10% off…</strong></div>';
    }
    else
    {
        return;
    }
}

You will have to set in WooCommerce coupon settings a coupon with the desire behavior (10% off) and you will report this coupon slug in this function.

Copy code in the functions.php file located in your active theme or better your active child theme.



来源:https://stackoverflow.com/questions/37686948/woocommerce-student-discount

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!