且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WooCommerce学生折扣

更新时间:2023-01-08 14:49:26

您可以在适当的条件下使用此自动将折扣优惠券应用于客户的购买功能(用户电子邮件由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.