WooCommerce学生折扣
问题描述:
我需要在WooCommerce中创建优惠券,该优惠券仅在用户的电子邮件以.ac.uk
结束时才有效。WooCommerce学生折扣
例如[email protected]
或[email protected]
。
折扣将是总购物篮10%。
我找不到任何解决方案在线或任何可以帮助的插件。
任何想法?
感谢。
答
您可以使用此自动(由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%折扣),您将在此功能中报告此优惠券。
将代码复制到位于活动主题中的functions.php文件中,或者更好地使用活动的儿童主题。