所以我有一个使用wordpress和woo-commerce的电子商务网站,现在使用智能优惠券woocommerce插件扩展,有一种方法可以让我创建一个信用优惠券/礼品卡(最初没有电子邮件限制),并在第一次使用礼品卡时手动分配当前用户的电子邮件作为它的电子邮件限制。
我正在尝试创建一个语音系统,使用礼品卡作为在我的商店购买产品的代金券。
发布于 2018-03-10 02:55:55
由于您没有任何代码来显示您正在使用的内容,请尝试以下代码。
$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。这个bug可能会在将来修复,但在那之前...
https://stackoverflow.com/questions/25291155
复制相似问题