WooCommerce学生折扣 [英] WooCommerce Student Discount

查看:82
本文介绍了WooCommerce学生折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在WooCommerce中创建优惠券,该优惠券仅在用户收到以.ac.uk结尾的电子邮件时才可用.

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

例如student.name@uwl.ac.ukteacher@kings.ac.uk.

折扣将是整个购物篮的10%.

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

我找不到任何在线解决方案或任何可以帮助您的插件.

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

有什么想法吗?

谢谢.

推荐答案

您可以在适当的条件下使用此自动将折扣优惠券应用于客户的购买功能(用户电子邮件由ac.uk完成):

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;
    }
}

您将必须在WooCommerce优惠券设置中设置具有期望行为的优惠券(优惠10%),并且您将在此功能中报告此优惠券.

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.

将代码复制到活动主题中或更好的活动子主题中的functions.php文件中.

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

这篇关于WooCommerce学生折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆