在woocommerce中手动对优惠券应用电子邮件限制的代码

Posted

技术标签:

【中文标题】在woocommerce中手动对优惠券应用电子邮件限制的代码【英文标题】:Code to manually apply an Email restriction on a coupon in woocommerce 【发布时间】:2014-08-13 16:13:03 【问题描述】:

所以我有一个使用 wordpress 和 woo-commerce 的电子商务网站,现在使用 smart-coupon woocommerce 插件扩展我有办法创建信用卡优惠券/礼品卡(最初没有电子邮件限制)并在第一次使用礼品卡时手动分配当前用户的电子邮件作为其电子邮件限制。

我正在尝试创建一个代金券系统,使用礼品卡作为在我的商店购买产品的代金券。

【问题讨论】:

我建议这是一个针对 woo-commerce 特定论坛的问题。 我在几天前尝试过,在那里无法获得任何帮助。所以现在我在这里。 【参考方案1】:

由于您没有任何代码来显示您正在使用的内容,请试试这个。

$coupon_code = 'some-code-I-created';
$user = wp_get_current_user();//Get the current user object
$coupon = new WC_Coupon( $coupon_code );//Get the coupon object
$emails = $coupon->get_email_restrictions();//Returns an empty array or array of emails
$emails[] = strtolower($user->billing_email);//Add user's billing email address to the array
$emails = array_filter($emails);//Remove empty values
array_unique($emails);//Remove any duplicate values
$coupon->set_email_restrictions($emails);//Set the coupon's array with the updated array
$coupon->save();//Save the coupon

如果我要批量添加电子邮件地址(例如使用 array_merge() 而不是一次添加一个地址),最好在更新之前通过删除任何空值或重复值来确保数组是干净的优惠券对象。

此外,WooCommerce 会比较所有小写的账单地址,但不会比较优惠券中的受限电子邮件,因此请确保在将其添加到优惠券时将其添加为全小写,以便正确验证。 Source。这个错误可能会在将来修复,但在那之前......

【讨论】:

以上是关于在woocommerce中手动对优惠券应用电子邮件限制的代码的主要内容,如果未能解决你的问题,请参考以下文章

即使在WooCommerce中购物车为空,也可以通过URL中的GET方法申请优惠券折扣

检查是不是在 woocommerce 中应用了任何优惠券代码

Woocommerce:如何在应用/删除优惠券时禁用 AJAX?

如果在 Woocommerce 中应用了任何优惠券代码,请删除一些支付网关

如何使用 woocommerce_order_item_meta_end 在电子邮件确认中显示项目元数据

根据在 WooCommerce 中应用的优惠券显示货到付款 (COD)