【问题标题】:Group Woocommerce users by Coupon按优惠券分组 Woocommerce 用户
【发布时间】:2019-05-19 00:36:17
【问题描述】:

希望一些传说可以为我指明正确的方向。

我想对购买时在 woocommerce 中使用某些优惠券代码的用户进行分组。我意识到有诸如 woocommerce 组之类的插件可以让您将优惠券限制为用户组,但我需要这种方式。

例如:某人在购买时使用优惠券代码“cricket”,这会将用户添加到“cricket-users”组中。

非常感谢V!

【问题讨论】:

  • 他们需要成为自己的用户角色还是添加用户元数据就足够了?
  • 不,他们不需要添加自己的组。将预先创建组,然后一旦他们使用特定代码购买,它就会自动将它们添加到该组中。
  • 你是如何创建群组的?
  • 刚刚使用 WP 中的用户角色编辑器创建了组。丹,你是明星!

标签: wordpress woocommerce


【解决方案1】:
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( 'cricket' ) ){

                //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');
                *
                *
                */
        }


    }
}

【讨论】:

    猜你喜欢
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    相关资源
    最近更新 更多