在woocommerce中:我想在客户下订单时将其分组。 我安装了“用户角色编辑器”插件来创建特定角色(例如:学生,老师等)
客户在第一次下单/注册时必须完成优惠券字段才能注册。 优惠券的内容例如是“学生优惠券”
如何根据优惠券设置客户的角色?
我尝试了这种方法:
Group Woocommerce users by Coupon
...但是我无法使其正常工作...
这是我的代码:
// ASSIGN GROUP FROM COUPON
add_action('woocommerce_thankyou', 'coupon_group', 10, 1);
function coupon_group( $order_id ){
$order = wc_get_order( $order_id );
foreach( $order->get_used_coupons() as $coupon_name ){
// Retrieving the coupon ID
$coupon_post_obj = get_page_by_title($coupon_name, OBJECT, 'shop_coupon');
$coupon_id = $coupon_post_obj->ID;
$coupons_obj = new WC_Coupon($coupon_id);
if( $coupons_obj->is_type( 'student' ) ){
//get the user id of the customer from the order
$user_id = $order->get_user_id();
/*
*
* logic that adds the user to the group cricket users
* i.e. $add_user_to_coupon_group = add_user_meta($user_id, 'custom_user_group', 'cricket_users');
*
*
*/
$add_user_to_coupon_group = add_user_meta($user_id, 'student', 'student_users');
}
}
}
优惠券标题为“学生”。
“用户角色编辑器”角色是学生(学生ID)。
...不确定我是否必须同时更改“ $ add_user_to_coupon_group”行上的“ custom_user_group”和“ cricket_users”或仅更改“ cricket_users”
也许有人看到更好的解决方案?
谢谢